iOS UINavigationController添加左右按鈕不顯示問題解決方案

來源:互聯網
上載者:User

標籤:

一。情境類比

我們有一個UINavigationController和一個UIViewController,在程式啟動時初始化這兩個,代碼如下

HomeViewController *homeCon = [[HomeViewController alloc] init];  // 初始化uiviewcontrollerUINavigationHomeBar *homeNavigation = [[UINavigationHomeBar alloc] initWithRootViewController:homeCon];            // 初始化uinavigationcontroller

接下來,當我們在uinavigationcontroller中加入左右按鈕時沒有效果,代碼如下

// 這個是uinavigationcontroller的viewdidload中UIBarButtonItem *bill = [[UIBarButtonItem alloc] initWithTitle:@"賬單" style:UIBarButtonItemStylePlain target:nil action:nil];self.navigationItem.rightBarButtonItem = bill;

運行看不見按鈕

二。問題解決

  1. 先查看navigationitem的說明

@property(nonatomic,readonly,retain) UINavigationItem *navigationItem; // Created on-demand so that a view controller may customize its navigation appearance.

說:在程式有需要時才建立,若有一個viewcontroller則可能屏蔽當前navigation中的item


2.解決方案

既然uinavigationcontroller中的item不能用,就需要使用uiviewcontroller中的item

即將建立按鈕寫入到uiviewcontroller的viewdidload中

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.title = @"標題";        // 添加賬單按鈕    UIBarButtonItem *bill = [[UIBarButtonItem alloc] initWithTitle:@"賬單" style:UIBarButtonItemStylePlain target:nil action:nil];    self.navigationItem.rightBarButtonItem = bill;}

三。navigation左右按鈕顏色與標題顏色不一樣的解決方案

在navigation的viewdidload中加入一句

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

即可


iOS UINavigationController添加左右按鈕不顯示問題解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.