之前自定義了navigationBar的背景顏色麻车,升級到iOS10后胞得,發(fā)現(xiàn)title,跟leftBarButtonItem不顯示慢洋。
iOS9 之前的navigationBar的背景是_UINavigationBarBackground垮抗,到iOS變成了_UIBarBackground刚陡,可以通過xcode查看一下相應(yīng)的布局惩妇。我的解決辦法是,分別判斷iOS10筐乳,iOS9的版本歌殃,找到對應(yīng)的背景圖,將起隱藏掉蝙云,可以消除分割線氓皱。然后重新創(chuàng)建一個(gè)視圖層,顏色可以自定義勃刨,需要注意在添加視圖或者更新視圖時(shí)要放到主線程里波材,同時(shí)這個(gè)方法需要在viewWillAppear里調(diào)用。
#define isIOS9 ([[UIDevice currentDevice].systemVersion intValue]>=9?YES:NO)
#define isIOS10 ([[UIDevice currentDevice].systemVersion intValue]>=10?YES:NO)
#pragma mark - 動(dòng)態(tài)修改狀態(tài)欄跟頂部導(dǎo)航欄的顏色
-(void)changeNavigationBarBackgroundColor:(UIColor *)barBackgroundColor{
if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){
NSArray *subviews =self.navigationController.navigationBar.subviews;
for (id viewObj in subviews) {
if (isIOS10) {
//iOS10,改變了狀態(tài)欄的類為_UIBarBackground
NSString *classStr = [NSString stringWithUTF8String:object_getClassName(viewObj)];
if ([classStr isEqualToString:@"_UIBarBackground"]) {
UIImageView *imageView=(UIImageView *)viewObj;
imageView.hidden=YES;
}
}else{
//iOS9以及iOS9之前使用的是_UINavigationBarBackground
NSString *classStr = [NSString stringWithUTF8String:object_getClassName(viewObj)];
if ([classStr isEqualToString:@"_UINavigationBarBackground"]) {
UIImageView *imageView=(UIImageView *)viewObj;
imageView.hidden=YES;
}
}
}
UIImageView *imageView = [self.navigationController.navigationBar viewWithTag:111];
if (!imageView) {
imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, -20, self.view.width, 64)];
imageView.tag = 111;
[imageView setBackgroundColor:barBackgroundColor];
dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationController.navigationBar insertSubview:imageView atIndex:0];
});
}else{
[imageView setBackgroundColor:barBackgroundColor];
dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationController.navigationBar sendSubviewToBack:imageView];
});
}
}
}