How to set titletext attributes for UINavigation panel for only one controller?

I am trying to change the font size of only one of my view controllers because it lends itself to more text, which requires a smaller font size than my other views. I set the navigation bar attributes in appdelegate during didFinishLaunchingWithOptions, and then set the font for my view controller in question during viewDidLoad. However, when I return to the screen or forward, the changes that I made in the navigation bar are saved, i.e. Smaller font. Is there any way around this? For example, if the font returns to normal after exiting the view, or something else?

Cliffsnotes: An attempt to install a font in only one view controller, but as soon as I installed it, it applies to all view controllers.

The code is as follows:

In AppDelegate:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault]; [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIColor clearColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset, [UIFont fontWithName:@"QuicksandBold-Regular" size:24.0], UITextAttributeFont, nil]]; 

In the controller view viewDidLoad:

 [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor clearColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset, [UIFont fontWithName:@"QuicksandBold-Regular" size:18.0], UITextAttributeFont, nil]]; 
+4
source share
2 answers

The navigator is shared by all view controllers in the navigation controller. Therefore, as soon as you change the navigation bar, all view controllers on the same stack will be affected. One option is to set a custom header for this one view controller.

 self.navigationItem.titleView = ... // some UILabel with the desired formatting 

Thus, only one view controller shows a custom title.

Another option would be to change the appearance of the navigator in viewWillAppear view controller mode, and then reset in viewWillDisappear view mode. But this approach may not be as smooth as just using a custom titleView.

+3
source

So now your assigned keys are out of date. Use these keys:

 NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIColor clearColor], NSForegroundColorAttributeName, [UIColor whiteColor], NSShadowAttributeName, [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], NSShadowAttributeName, [UIFont fontWithName:@"QuicksandBold-Regular" size:24.0], NSFontAttributeName, nil]; 
0
source

Source: https://habr.com/ru/post/1438765/


All Articles