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