iOS UINavigationController 添加導航欄左右按鈕不顯示解決方案

  • 場景模擬:
    點擊一個按鈕,彈出一個導航視圖。導航視圖中包含左按鈕:取消按鈕;右按鈕:存儲信息(什么亂起八糟的隨你)

  • 出現(xiàn)問題:
    為導航視圖設置導航欄的左右按鈕后不顯示。

有一個UIViewController 和一個UINavigationController:

UIViewController* controller = [[UIViewController alloc]init];
 UINavigationController* navigationController = [[UINavigationController alloc]initWithRootViewController:controller];

設置導航欄的左右按鈕:

UIBarButtonItem* leftButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(clickCancel)];
    UIBarButtonItem* rightButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self
    action:@selector(clickSave)];
navigationController.navigationItem.leftBarButtonItem=leftButtonItem; navigationController.navigationItem.rightBarButtonItem=rightButtonItem;

展現(xiàn)該導航視圖:

[self presentViewController:navigationController animated:YES completion:nil];

發(fā)現(xiàn)導航視圖并不現(xiàn)實左右按鈕,command+點擊 進入navigationItem查看其聲明和注釋:

@property(nonatomic,readonly,strong) UINavigationItem *navigationItem; // Created on-demand so that a view controller may customize its navigation appearance.```
大概意思是:在程序有需要時才創(chuàng)建,若有一個viewcontroller則可能屏蔽當前navigation中的item


* 解決方案:

將
` navigationController.navigationItem.leftBarButtonItem=leftButtonItem; navigationController.navigationItem.rightBarButtonItem=rightButtonItem;`
改為:
`
controller.navigationItem.leftBarButtonItem=leftButtonItem;
controller.navigationItem.rightBarButtonItem=rightButtonItem;
`
即可(本人不準確地理解為:UINavigationController并不具有view,因此需要擁有rootViewController即UIViewController,因此其外觀也被UIViewController所決定,所以需要設置UIViewController來設置NavigationController)。


* 貼上完整代碼:

-(IBAction)click:(id)sender {
UIViewController* controller = [[UIViewController alloc]init];
UINavigationController* navigationController = [[UINavigationController alloc]initWithRootViewController:controller];
UIBarButtonItem* leftButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(clickCancel)];
UIBarButtonItem* rightButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self
action:@selector(clickSave)];
controller.title=@"damn";
controller.navigationItem.leftBarButtonItem=leftButtonItem;
controller.navigationItem.rightBarButtonItem=rightButtonItem;
[navigationController.view setBackgroundColor:[UIColor whiteColor]];
[self presentViewController:navigationController animated:YES completion:nil];
}
-(void)clickCancel{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)clickSave{
NSLog(@"save successfully");
}



運行截圖:

![
![2.png](http://upload-images.jianshu.io/upload_images/2920598-43d6d75dda8354cb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)](http://upload-images.jianshu.io/upload_images/2920598-407c584f1e8b4449.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容