設置一個按鈕的方法就不再贅述了 法一: UIBarButtonItem *oneButton = [[UIBarButtonItemalloc]initWithTitle:@"設置" style:UIBarButtonItemStylePlain target:selfaction:@selecto ...
設置一個按鈕的方法就不再贅述了
- 設置多個按鈕
法一:
UIBarButtonItem *oneButton = [[UIBarButtonItemalloc]initWithTitle:@"設置" style:UIBarButtonItemStylePlain
target:selfaction:@selector(oneButtonAction:)];
UIBarButtonItem *twoButton = [[UIBarButtonItemalloc]initWithTitle:@"消息" style:UIBarButtonItemStyleDone target:selfaction:@selector(twoButtonAction:)];
NSArray *actionButtonItems = @[oneButton, twoButton];
self.navigationItem.rightBarButtonItems = actionButtonItems;
註:以上的style參數,我寫的是UIBarButtonItemStylePlain和UIBarButtonItemStyleDone兩種方式,UIBarButtonItemStyleDone預設字體為系統按鈕的字體的設置(加粗),UIBarButtonItemStylePlain預設字體為lable的字體設置(17號字,無加粗)
法二:(此法本人不推薦,因為在點擊時,兩個按鈕的背景(即UIToolbar的背景)都會有常亮顯示)
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, WIDTHINIPHONE6(95), 44)];
[tools setBarTintColor:DOMINANTCOLOR];
[tools setTintColor:WHITEBACK];
[tools setAlpha:[self.navigationController.navigationBar alpha]];
UIBarButtonItem *setBtn = [[UIBarButtonItem alloc] initWithTitle:@"設置" style:UIBarButtonItemStylePlain target:self action:@selector(setUserSetting)];
UIBarButtonItem *msgBtn = [[UIBarButtonItem alloc] initWithTitle:@"消息" style:UIBarButtonItemStylePlain target:self action:@selector(lookUserMsg)];
NSArray *btnArr = @[setBtn,msgBtn];
[tools setItems:btnArr];
UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:tools];
2. 背景圖
[navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
3. 圖片與文字按鈕(這裡我以自定義左部按鈕為例)
_leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];_leftBtn.frame = CGRectMake(0, 0, 60, 25);
[_leftBtn setTitle:@"返回" forState:UIControlStateNormal];
[_leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
_leftBtn.titleLabel.font = HEADFONT;
[_leftBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
_leftBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
_leftBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:_leftBtn]; self.navigationItem.leftBarButtonItem = leftButton;
參考:http://blog.csdn.net/lengshengren/article/details/16820803