我在设置图像方面遇到了一些困难。这有点奇怪,因为在不同的控制器中它确实有效。可能self.navigationItem
无效,但是为什么我只是从不同的ViewController
推送ViewController
?
我试过了:
// preview buttonImage shows that image is correct and exists
UIImage *buttonImage = [[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithTitle:@"TEST" style:UIBarButtonItemStylePlain target:nil action:NULL];
[buttonItem setBackButtonBackgroundImage:buttonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.navigationItem.backBarButtonItem = buttonItem;
不同的方法:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"TEST" forState:UIControlStateNormal];
[button setImage:buttonImage forState:UIControlStateNormal];
buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.backBarButtonItem = buttonItem;
我认为也许self.navigationItem是错误的,但似乎存在:
NSLog(@"%@, %@", self.navigationItem, self.navigationItem.backBarButtonItem);
--CONSOLE-- MyApp[56574:3827402] <UINavigationItem: 0x7b6ecd10>, <UIBarButtonItem: 0x7b6f01c0>
我甚至试图设置:
self.navigationItem.backBarButtonItem = nil;
似乎也没有用。
- 编辑 -
但是,以下代码可以工作:
[self.navigationItem setHidesBackButton:YES];
有什么建议,self.navigationItem
是有效的。
答案 0 :(得分:0)
UIImage *backImage = [UIImage imageNamed:@"back"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backImage forState:UIControlStateNormal barMetrics:UIBarMetricsCompact];
因此我需要为此控制器更改它,我想使用不同的图像。