iPhone导航栏标题文本颜色在iOS 11中的功能viewWillAppear()或viewWillDisappear中不会更改 [英] iPhone Navigation Bar Title text color do not change in function viewWillAppear() or viewWillDisappear in iOS 11

查看:209
本文介绍了iPhone导航栏标题文本颜色在iOS 11中的功能viewWillAppear()或viewWillDisappear中不会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 10中未发生该错误。
标题文本颜色的默认值为黑色,当导航至新屏幕时(2)我将viewWillAppear()中的标题文本颜色更改为粉红色。 viewWillDisappear我将其更改为默认颜色。
逻辑在iOS 10上是可以的,但在iOS 11上,具有标题颜色的第一个屏幕是粉红色(预期是默认颜色)



另外:当在viewWillAppear()中添加逻辑更改颜色时(这种情况下颜色不会更改),但是在viewDidAppear()中工作此颜色,但有错误,标题为闪烁,返回时更改颜色从屏幕2到屏幕1



屏幕2中的源(适用于iOS 10):

  #define NAVBAR_TITLE_FONT_ATTR @ {UITextAttributeFont:[UIFont boldSystemFontOfSize:19],UITextAttributeTextColor:[UIColor colorWithRed:9 / 255.0 green:34 / 255.0 blue:83 / 255.0 alpha:1]} 
#define NAVBAR_TINT_COLOR [UIColor colorWithRed:97 / 255.0 green:113 / 255.0 blue:146 / 255.0 alpha:1]
#define NAVBAR_BG_COLOR [UIColor colorWithRed:247 / 255.0 green:247 / 255.0 blue:247 / 255.0 alpha:1]
#定义LIGHT_BLUE_COLOR [UIColor colorWithRed:0.04绿色:0.13蓝色:0.33 alpha:1.0]


-(void)viewWil lAppear:(BOOL)动画{

[super viewWillAppear:animated];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage新];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
textColor = [UIColor pinkColor]
self.navigationController.navigationBar.tintColor = textColor;
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
textColor,NSForegroundColorAttributeName,
[UIFont boldSystemFontOfSize:19],NSFontAttributeName,nil];

}

-(void)viewWillDisappear:(BOOL)animated {

[super viewWillDisappear:animated];

self.navigationController.navigationBar.tintColor = NAVBAR_TINT_COLOR;
self.navigationController.navigationBar.barTintColor = NAVBAR_BG_COLOR;
self.navigationController.navigationBar.translucent = NO;
[self.navigationController.navigationBar setTitleTextAttributes:NAVBAR_TITLE_FONT_ATTR];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}







正确的答案是:

 -(void)willMoveToParentViewController: (UIViewController *)parent {

if(!parent){
self.navigationController.navigationBar.titleTextAttributes = @ {
NSForegroundColorAttributeName:[UIColor blackColor]
};

}

}

感谢@Phu阮




The error did not happen in iOS 10. The default of title text color is black color, when navigate to new screen (2) i change the title text color to pink color in viewWillAppear(), and in viewWillDisappear i change this to default color. The logic is ok with iOS 10, but with iOS 11 the first screen that have the bar title color is pink color (the expected is default color)

In addition : when add logic change color in viewWillAppear() (the color do not change in this situation) however this work in viewDidAppear(),but there is error, the title is blink change color when back from screen 2 to screen 1

source in screen 2 (work for iOS 10):

#define NAVBAR_TITLE_FONT_ATTR @{ UITextAttributeFont : [UIFont boldSystemFontOfSize:19], UITextAttributeTextColor: [UIColor colorWithRed:9/255.0 green:34/255.0 blue:83/255.0 alpha:1]}
#define NAVBAR_TINT_COLOR [UIColor colorWithRed:97/255.0 green:113/255.0 blue:146/255.0 alpha:1]
#define NAVBAR_BG_COLOR [UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1]
#define LIGHT_BLUE_COLOR [UIColor colorWithRed:0.04 green:0.13 blue:0.33 alpha:1.0]


-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new]                                              forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.view.backgroundColor = [UIColor clearColor];
    textColor = [UIColor pinkColor] 
    self.navigationController.navigationBar.tintColor = textColor;
    self.navigationController.navigationBar.titleTextAttributes =  [NSDictionary dictionaryWithObjectsAndKeys:
                                                                    textColor, NSForegroundColorAttributeName,
                                                                    [UIFont boldSystemFontOfSize:19], NSFontAttributeName,nil];

}

-(void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];

    self.navigationController.navigationBar.tintColor = NAVBAR_TINT_COLOR;
    self.navigationController.navigationBar.barTintColor = NAVBAR_BG_COLOR;
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setTitleTextAttributes:NAVBAR_TITLE_FONT_ATTR];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

The correct answer is :

- (void)willMoveToParentViewController:(UIViewController *)parent {

    if (!parent) {
        self.navigationController.navigationBar.titleTextAttributes =  @{
                                                                         NSForegroundColorAttributeName: [UIColor blackColor]
                                                                         };

 }

}

thanks @Phu Nguyen

Detecting when the 'back' button is pressed on a navbar

解决方案

Have you tried this in second view controller?

- (void)willMoveToParentViewController:(UIViewController *)parent {
  [super willMoveToParentViewController:parent];
    NSLog(@"Parent view controller: %@", parent);
    if (!parent) {
        self.navigationController.navigationBar.titleTextAttributes =  @{
                                                                         NSForegroundColorAttributeName: [UIColor blackColor]
                                                                         };
    }
}

这篇关于iPhone导航栏标题文本颜色在iOS 11中的功能viewWillAppear()或viewWillDisappear中不会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆