我试图在导航栏中设置我的图像而不是标准后退箭头。它看起来像
UIImage *tmpImage = [UIImage imageNamed:@"back.png"];
CGSize newSize = CGSizeMake(12, 22);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0f);
[tmpImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *backButtonImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[UIBarButtonItem.appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -64) forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackIndicatorImage:backButtonImage];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backButtonImage];
问题有时是有效的,有时候不行。此外,它取决于构建,但不取决于发布。我的意思是,在一个构建中,它可能会起作用,在重建之后(即使没有任何变化)它也可能不起作用。如果我的后退按钮在某些版本中有效,那么它无需考虑我启动应用程序的次数。
有人有想法如何修复它吗? 谢谢!
修改 我尝试使用UIGraphicsGetImageFromCurrentImageContext(); 结果是一样的......
答案 0 :(得分:0)
我试图从AppDelegate.m做到这一点,但我无法做到。 最后我制作了静态方法,生成如下按钮:
+(UIBarButtonItem*) backButton: (id)target selector:(SEL)selector{
UIViewController* controller=(UIViewController*)target;
[controller.navigationController setNavigationBarHidden:NO];
UIImage* backImg = [UIImage imageNamed:@"backbtniphonetrimed.png"];
UIButton* backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setFrame:CGRectMake(0, 0, backImg.size.width , backImg.size.height)];
[backBtn setImage:backImg forState:UIControlStateNormal];
[backBtn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
if ([UIImage imageNamed:@"backbtniphonetrimed.png"] != nil){
[backBtn setImage:[UIImage imageNamed:@"backbtniphonetrimed.png"] forState:UIControlStateHighlighted];
}
return [[UIBarButtonItem alloc] initWithCustomView:backBtn];
}
然后在viewDidLoad方法的控制器中,我只是调用它并实现了返回方法。
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem=[UIHelper backButton:self selector:@selector(back)];
}
-(void) back{
[self.navigationController popViewControllerAnimated:YES];
}