方法一:
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
[tools setBarTintColor:[UIColor whiteColor]];
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UITabBarSystemItemContacts target:self action:@selector(save:)];
UIBarButtonItem *pulishButton = [[UIBarButtonItem alloc]initWithTitle:@"发布" style:UITabBarSystemItemContacts target:self action:@selector(pulish:)];
[pulishButton setTintColor:kMainColor];
[buttons addObject:saveButton];
[buttons addObject:pulishButton];
[tools setItems:buttons animated:NO];
UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = myBtn;方法二:
UIBarButtonItem *pulishButton = [[UIBarButtonItem alloc]initWithTitle:@"发布" style:UITabBarSystemItemContacts target:self action:@selector(pulish:)];
[pulishButton setTintColor:kMainColor];
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UITabBarSystemItemContacts target:self action:@selector(save:)];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects: pulishButton,saveButton,nil]];
方法三:
UIButton *pulishButton=[UIButton buttonWithType:(UIButtonTypeCustom)];
[pulishButton setTitle:@"发布" forState:(UIControlStateNormal)];
[pulishButton setTitleColor:kWenZiColor forState:(UIControlStateNormal)];
pulishButton.layer.masksToBounds=YES;
pulishButton.layer.cornerRadius=3;
pulishButton.titleLabel.font=[UIFont systemFontOfSize:15];
pulishButton.backgroundColor=[UIColor cyanColor];
[pulishButton addTarget:self action:@selector(pulish:) forControlEvents:UIControlEventTouchUpInside];
UIButton *saveButton=[UIButton buttonWithType:(UIButtonTypeCustom)];
[saveButton setTitle:@"保存" forState:(UIControlStateNormal)];
[saveButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
saveButton.layer.masksToBounds=YES;
saveButton.layer.cornerRadius=3;
saveButton.titleLabel.font=[UIFont systemFontOfSize:15];
saveButton.backgroundColor=kMainColor;
[saveButton addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
pulishButton.frame = CGRectMake(0, 0, 50, 30);
saveButton.frame=CGRectMake(0, 0, 50, 30);
UIBarButtonItem *pulish = [[UIBarButtonItem alloc] initWithCustomView:pulishButton];
UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithCustomView:saveButton];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects: pulish, save,nil]];个人认为 方法三最好= = 三种方法供大家参考 如果有更好的 欢迎补充与分享哦= =
本文介绍了三种在iOS应用中为工具栏添加按钮的方法。方法一使用`UIToolbar`并设置多个按钮;方法二直接通过`UIBarButtonItem`设置右侧按钮项;方法三自定义按钮样式并通过`UIBarButtonItem`添加到导航栏。
2506

被折叠的 条评论
为什么被折叠?



