本帖最后由 竹林风 于 2018-12-7 18:22 编辑
ios导航栏rightBarButtonItems多个按钮自定义设置 的方式有多种:
1.带图片的按钮
[Objective-C] 纯文本查看 复制代码 #import "firstVC.h"
@interface firstVC (){
UILabel *lbl;
}
@end
@implementation firstVC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
self.title = @"Leader";
// 带图片的按钮
UIButton *informationCardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[informationCardBtn addTarget:self action:@selector(mineButtonAction) forControlEvents:UIControlEventTouchUpInside];
[informationCardBtn setImage:[UIImage imageNamed:@"mine"] forState:UIControlStateNormal];
[informationCardBtn sizeToFit];
UIBarButtonItem *informationCardItem = [[UIBarButtonItem alloc] initWithCustomView:informationCardBtn];
UIBarButtonItem *fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedSpaceBarButtonItem.width = 22;
UIButton *settingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[settingBtn addTarget:self action:@selector(homeButtonAction) forControlEvents:UIControlEventTouchUpInside];
[settingBtn setImage:[UIImage imageNamed:@"home"] forState:UIControlStateNormal];
[settingBtn sizeToFit];
UIBarButtonItem *settingBtnItem = [[UIBarButtonItem alloc] initWithCustomView:settingBtn];
self.navigationItem.rightBarButtonItems = @[informationCardItem,fixedSpaceBarButtonItem,settingBtnItem];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 180, 50)];
lbl.textColor = [UIColor blueColor];
[self.view addSubview:lbl];
}
-(void)mineButtonAction{
lbl.text = @"点击了我的按钮";
}
-(void)homeButtonAction{
lbl.text = @"点击了主页按钮";
}
看效果:
2.添加多个文字描述的按钮
[Objective-C] 纯文本查看 复制代码 UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"register", nil) style:UIBarButtonItemStylePlain target:self action:@selector(registerClick)];
UIBarButtonItem *anotherButton2 = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"login", nil) style:UIBarButtonItemStylePlain target:self action:@selector(loginClick)];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects: anotherButton,anotherButton2,nil]];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 180, 50)];
lbl.textColor = [UIColor blueColor];
[self.view addSubview:lbl];
[Objective-C] 纯文本查看 复制代码 -(void)registerClick{
lbl.text = @"点击了注册按钮";
}
-(void)loginClick{
lbl.text = @"点击了登录按钮";
}
看效果:
附件:
Test-自定义UIBarButtonItem.zip
(159.9 KB, 下载次数: 0)
|