UINavigationBar title behaves oddly when specifying a font using UIAppearance (with iOS 5 only)

谁说胖子不能爱 提交于 2019-12-06 08:37:45

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]
}];

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!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!