iOS 自定义NavigationBar右侧按钮rightBarButtonItem


自定义右侧的一个按钮

1 UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"主页" style:UIBarButtonItemStyleBordered target:self action:@selector(clickEvent)];
2 
3 self.navigationItem.rightBarButtonItem = myButton;

自定义右侧多个按钮

 1 //两个按钮的父类view
 2 
 3 UIView *rightButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
 4 
 5 //历史浏览按钮
 6 
 7 UIButton *historyBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
 8 
 9 [rightButtonView addSubview:historyBtn];
10 
11 [historyBtn setImage:[UIImage imageNamed:@"button_history"] forState:UIControlStateNormal];
12 
13 [historyBtn addTarget:self action:@selector(historyBtnEvent) forControlEvents:UIControlEventTouchUpInside];
14 
15 #pragma mark >>>>>主页搜索按钮
16 
17 //主页搜索按钮
18 
19 UIButton *mainAndSearchBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 0, 50, 50)];
20 
21 [rightButtonView addSubview:mainAndSearchBtn];
22 
23 [mainAndSearchBtn setImage:[UIImage imageNamed:@"button_filter-"] forState:UIControlStateNormal];
24 
25 [mainAndSearchBtn addTarget:self action:@selector(mainAndSearchBtnEvent) forControlEvents:UIControlEventTouchUpInside];
26 
27 //把右侧的两个按钮添加到rightBarButtonItem
28 
29 UIBarButtonItem *rightCunstomButtonView = [[UIBarButtonItem alloc] initWithCustomView:rightButtonView];
30 
31 self.navigationItem.rightBarButtonItem = rightCunstomButtonView;

 

转自:https://www.jianshu.com/p/901203a8a90e


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM