iOS11导航栏返回按钮错乱坑

以前导航栏是在AppDelegate中设置的:如下

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7&&[[[UIDevice currentDevice] systemVersion] floatValue] < 8)

{

[UINavigationBar appearance].tintColor =  [UIColor colorWithRed:(double)248/255 green:(double)183/255 blue:(double)43/255 alpha:1];//248 183 43  255 183 44

}

else  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)

{

[UINavigationBar appearance].tintColor = [UIColor whiteColor];

//自定义返回按钮

UIImage *backButtonImage = [[UIImage imageNamed:@"nav_back"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:17]}];

}

但是IOS11后,妖孽出现了,这么设置完全错乱,如下图


iOS11导航栏返回按钮错乱坑_第1张图片


解决如下,在每个VC中单独自定义

方法一

UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"nav_back"] style:UIBarButtonItemStylePlain target:self action:@selector(popToLastVC)];

self.navigationItem.leftBarButtonItem=item;

方法二:

UIButton *leftButton = [[UIButton alloc]init];

leftButton.frame = CGRectMake(0, 0, 20, 20);

[leftButton setBackgroundImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];

[leftButton addTarget:self action:@selector(popToLastVC) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *leftBarButtonItems = [[UIBarButtonItem alloc]initWithCustomView:leftButton];

//    //解决按钮不靠左 靠右的问题.

//    UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace

//                                                                                  target:nil action:nil];

//    nagetiveSpacer.width = -15;//这个值可以根据自己需要自己调整

self.navigationItem.leftBarButtonItems = @[leftBarButtonItems];

返回实现:

-(void)popToLastVC{

[self.navigationController popViewControllerAnimated:YES];

}



你可能感兴趣的:(iOS11导航栏返回按钮错乱坑)