I am using UIAppearance to set the font for my UINavigation bar title across my app.
If I don't set an appearance font, the title is there 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 using some sort of animated transition on loading on iOS 5. It also sometimes stalls (?) and only displays the first character of the title. If I tab away and back again, the title is displayed correctly.
I see this problem on iOS 5 only, and setting the title with a font behaves correctly 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 appearance in the AppDelegate, as follows...
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:FONT_OF_ALL_KNOWLEDGE size:0.0f], UITextAttributeFont,
nil]];
The title for the bar is set in 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've tried setting the font explicitly in viewDidLoad (just before setting the title), rather than making use of UIAppearance, and I still see the same problem, and still on iOS 5 only.
Had a similar problem. My UINavigationBar
s would only show one character of the title when being set in code (self.title = @"Title"
) on iOS < 6.0.
I fixed this by using a font size greater than 0.0
:
[[UINavigationBar appearance] setTitleTextAttributes:@{
UITextAttributeFont : [UIFont fontWithName:@"HelveticaNeue-Medium" size:20.0]
}];
I finally resolved 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, configure the text attributes the way you want them to be. Then, in all of my view controller's viewDidLoad
method 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 it works for you too!
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 seems not well, but work!
来源:https://stackoverflow.com/questions/12504556/uinavigationbar-title-behaves-oddly-when-specifying-a-font-using-uiappearance-w