UINavigationBar title behaves oddly when specifyin

2019-05-24 21:05发布

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.

3条回答
▲ chillily
2楼-- · 2019-05-24 21:44

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!

查看更多
做个烂人
3楼-- · 2019-05-24 21:52

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!

查看更多
混吃等死
4楼-- · 2019-05-24 22:00

Had a similar problem. My UINavigationBars 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]
}];
查看更多
登录 后发表回答