iPhone Navigation Bar Title text color do not chan

2019-06-20 17:28发布

问题:

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

回答1:

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]
                                                                         };
    }
}



回答2:

In your 1st screen viewWillAppear(), keep title text color is black color and in 2nd screen viewWillAppear(), keep it to pink color.



回答3:

I think you was changed your navigationbarTitleColor on Screen2. Remove the navigationBar Appearance code and check it.

(OR)

If you need to change the navigationBarTitleColor on Screen2 means. you have to update the navigationBarTitleColor on Screen1 in viewWillAppear



回答4:

SWIFT 4: Implement this function in second viewcontroller

override func willMove(toParent parent: UIViewController?) {
    let attrs = [
        NSAttributedString.Key.foregroundColor: UIColor.white,
        NSAttributedString.Key.font: UIFont(name: "MyriadPro-Regular", size: 22)!
    ]
    navigationController?.navigationBar.titleTextAttributes = attrs
}