7

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

Lang Le
  • 71
  • 5

4 Answers4

11

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

Phu Nguyen
  • 261
  • 3
  • 6
  • 2
    Are you sure that there is no other solution for this? I have the same issue and in my case the popping view controller doesn't know the color of the navigation bar of the previous view controller (as you can push it from several places in the app). The ideal solution for me would be to update the color in the particular view controller itself. However, setting it in viewWillAppear doesn't work. Resetting the color in the popped view controller looks a little clumsy to me... – croX Mar 21 '18 at 08:26
  • I don't know how you came across this solution, but this is crazy! Thank you ❤️ – Guy Kogus Apr 02 '19 at 10:48
0

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

Kathiresan Murugan
  • 2,783
  • 3
  • 23
  • 44
0

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

Ashvini
  • 342
  • 3
  • 11
  • It will helpful to understand your actual requirement if you share your code & explain your need more clearly. – Ashvini Jan 02 '18 at 06:31
  • i just update source code and flow logic for easy to discuss @Ashvini Wable thanks for help – Lang Le Jan 02 '18 at 07:47
0

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
}
Amrit Sidhu
  • 1,870
  • 1
  • 18
  • 32