The UINavigationBar header behaves strangely when specifying a font using UIAppearance (iOS 5 only)

I am using UIAppearance to set the font for the title of the UINavigation panel in my application.

If I do not set the appearance font, the title is immediately as expected, and its presentation is not animated.

However, when I specify an alternative font using UIAppearance, the title appears (with the specified font), but appears with some kind of animated transition when loading on iOS 5. It also sometimes stops (?) And displays only the first character of the name. If I leave and go back, the title will display correctly.

I see this problem only on iOS 5, and setting the title with the correct font display on iOS 6.

This problem can also be seen on the simulator for iOS 5 and again behaves correctly with iOS 6 in the simulator.

I set the look in AppDelegate as follows:

[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:FONT_OF_ALL_KNOWLEDGE size:0.0f], UITextAttributeFont, nil]]; 

The title for the panel is set to viewDidLoad for each of the [tabbed] views,

 - (void)viewDidLoad { [super viewDidLoad]; [self.navigationItem setTitle:@"Title"]; } 

Has anyone else seen this problem and is there a fix? Thanks.

UPDATE

I tried to set the font explicitly in viewDidLoad (just before setting the title), instead of using UIAppearance, and I still see the same problem and still on iOS 5.

+2
source share
3 answers

There was a similar problem. My UINavigationBar will only show one title character when set in code ( self.title = @"Title" ) on iOS <6.0.

I fixed this using a font size greater than 0.0 :

 [[UINavigationBar appearance] setTitleTextAttributes:@{ UITextAttributeFont : [UIFont fontWithName:@"HelveticaNeue-Medium" size:20.0] }]; 
0
source

I finally solved the problem by creating the following UIViewController category:

 #import <UIKit/UIKit.h> @interface UIViewController (A4UExtras) + (NSDictionary *)defaultTitleTextAttributes; @end #import "UIViewController+A4UExtras.h" @implementation UIViewController (A4UExtras) + (NSDictionary *)defaultTitleTextAttributes { NSDictionary *navBarTextAttributes = @{ UITextAttributeTextColor : [UIColor whiteColor], UITextAttributeFont : [UIFont fontWithName:@"Avenir-Book" size:17.0], UITextAttributeTextShadowColor : [UIColor colorWithRed:0 green:0 blue:0 alpha:.35], UITextAttributeTextShadowOffset : @1, }; return navBarTextAttributes; } @end 

Of course, adjust the text attributes the way you want. Then, in my entire cursor control method viewDidLoad I do the following:

 #import "UIViewController+A4UExtras.h" … - (void)viewDidLoad { [super viewDidLoad]; // NavBar appearance and setup self.navigationController.navigationBar.titleTextAttributes = [UIViewController defaultTitleTextAttributes]; self.navigationItem.title = @"My Title"; } 

And it works like a charm! Hope this works for you too!

+1
source

Use this code:

 [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:FONT_OF_ALL_KNOWLEDGE size:0.0f], UITextAttributeFont, nil]]; // Present a temp UIViewController UIViewController *vc = [[UIViewController alloc]init]; [self presentViewController:vc animated:NO completion:nil];//"self" is an instance of UIViewController [vc dismissViewControllerAnimated:NO completion:nil]; 

It doesn't seem very good, but work!

0
source

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


All Articles