隐藏自定义导航栏按钮的导航栏中的自定义图像
问题描述:
首先我解释我在做什么。我创建了基于导航的应用程序。我必须在导航栏中添加一个自定义图像。我通过使用添加图像 -隐藏自定义导航栏按钮的导航栏中的自定义图像
[self.navigationController.navigationBar insertSubview:image atIndex:0]
;
之后,我添加了两个自定义按钮的左侧和右侧到同一视图的导航栏。我有另一个视图,并在此视图上我还添加了两个自定义按钮左右导航栏。一切都很好,直到现在,但当我导航到我的第二个视图我的自定义按钮,我添加到viewwillappear导航控制器不显示。我使用此代码将自定义按钮添加到导航栏 -
UIBarButtonItem *customHome = [[UIBarButtonItem alloc] initWithCustomView: buttonHome];
[self.navigationController.navigationItem setLeftBarButtonItem:customHome];
请建议这里有什么问题。 :(
答
你试图设置navigationItem
导航控制器,而不是视图控制器上。
答
如果您已嵌入您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
之间的区别,它管理UINavigationBar
和UINavigationItem
,您和UINavigationController
共同管理。 (不要忘记UINavigationBarDelegate
协议,它有很少使用但有用的方法。)