Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone Navigation Bar Title text color do not change in function viewWillAppear() or viewWillDisappear in iOS 11

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

screen 1

screen 2

come back screen 1

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

like image 825
Lang Le Avatar asked Jan 02 '18 04:01

Lang Le


1 Answers

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

enter image description here

like image 137
Phu Nguyen Avatar answered Sep 19 '22 15:09

Phu Nguyen