我有一个带有NavigationController的故事板,它实现了我自己的NavigationViewController。
这是我在NavigationViewController中使用的设置代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[self setNavigationStyle];
}
- (void) setNavigationStyle
{
self.navigationBar.backgroundColor = [[UIColor alloc]initWithRed:245 green:246 blue:247 alpha:1];
self.navigationBar.hidden = NO;
self.title = @"test";
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
NSArray *actionButtonItems = @[shareItem, cameraItem];
self.navigationItem.rightBarButtonItems = actionButtonItems;
}
backgroundColor设置有效,导航控制器背景颜色也会更改。 但是在尝试添加按钮或标题时,它不会影响导航栏。
我做错了什么?
更新: 我已经实现了UiNavigtaionControlerDelegate:
- (void) setNavigationStyle: (UINavigationController *)navigationController
{
navigationController.navigationBar.backgroundColor = [[UIColor alloc]initWithRed:245 green:246 blue:247 alpha:1];
navigationController.navigationBar.hidden = NO;
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
NSArray *actionButtonItems = @[shareItem, cameraItem];
navigationController.navigationItem.rightBarButtonItems = actionButtonItems;
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[self setNavigationStyle:navigationController];
}
仍然无效
答案 0 :(得分:0)
根据类似问题的答案(Similar question)
虽然导航控制器有一个navigationItem,但实际上并非如此 用过的。您应该重写导航控制器方法以推送视图 控制器(或使用委托方法)来修改 每个新视图控制器的navigationItem被推送。
实现UINavigationControllerDelegate和navigationController:willShowViewController:动画方法,然后更改navigationItem属性。
答案 1 :(得分:0)
试试这个,创建导航栏和导航项目的插座,然后删除标题,按钮,颜色等。在IB
- (void) setNavigationStyle
{
self.navigationBar.backgroundColor = [[UIColor alloc]initWithRed:245 green:246 blue:247 alpha:1];
self.navigationBar.hidden = NO;
self.navigationItem.title = @"test";
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
NSArray *actionButtonItems = @[shareItem, cameraItem];
self.navigationItem.rightBarButtonItems = actionButtonItems;
}