0

首先我解釋我在做什麼。我創建了基於導航的應用程序。我必須在導航欄中添加一個自定義圖像。我通過使用添加圖像 -隱藏自定義導航欄按鈕的導航欄中的自定義圖像

[self.navigationController.navigationBar insertSubview:image atIndex:0];

之後,我添加了兩個自定義按鈕的左側和右側到同一視圖的導航欄。我有另一個視圖,並在此視圖上我還添加了兩個自定義按鈕左右導航欄。一切都很好,直到現在,但當我導航到我的第二個視圖我的自定義按鈕,我添加到viewwillappear導航控制器不顯示。我使用此代碼將自定義按鈕添加到導航欄 -

UIBarButtonItem *customHome = [[UIBarButtonItem alloc] initWithCustomView: buttonHome]; 
[self.navigationController.navigationItem setLeftBarButtonItem:customHome]; 

請建議這裏有什麼問題。 :(

回答

0

你試圖設置navigationItem導航控制器,而不是視圖控制器上。

0

如果您已嵌入您UIViewController在視圖控制器的viewDidLoad一個UINavigationController那麼你可以,例如,使標題UISearchBar(你在Xcode IB拖到UIViewController),換上這樣的右邊2個按鈕:

UIBarButtonItem *componentsButton = [[UIBarButtonItem alloc] initWithTitle:@"components" style:UIBarButtonItemStylePlain target:self action:@selector(componentsButtonTapped:)]; 
    UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithTitle:@"settings" style:UIBarButtonItemStylePlain target:self action:@selector(settingsButtonTapped:)]; 
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:componentsButton, settingsButton, nil]; 
    self.navigationItem.titleView = searchBar; 

這裏的問題是UINavigationController之間的區別,它管理UINavigationBarUINavigationItem,您和UINavigationController共同管理。 (不要忘記UINavigationBarDelegate協議,它有很少使用但有用的方法。)