【问题标题】:How to style text in navigation bar in iOS 7?如何在 iOS 7 中设置导航栏中的文本样式?
【发布时间】:2014-03-26 04:47:36
【问题描述】:

我正在尝试使用以下代码设置导航栏文本的样式,但无法使其正常工作。我之前在这里有代码,但 Xcode 一直告诉我它已被 iOS 7 弃用。关于如何让它工作的任何建议?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self.navigationController setNavigationBarHidden:NO];

    //SET COLORS AND FONTS
    UIColor* mainColor = [UIColor colorWithRed:28.0/255 green:158.0/255 blue:121.0/255 alpha:1.0f];
    UIColor* darkColor = [UIColor colorWithRed:7.0/255 green:61.0/255 blue:48.0/255 alpha:1.0f];

    NSString* fontName = @"Avenir-Book";
    NSString* boldFontName = @"Avenir-Black";

    //[self styleNavigationBarWithFontName:boldFontName];

    self.title = @"Walk With Me";

    UINavigationBar* navAppearance = [UINavigationBar appearance];

    [navAppearance setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                           [UIColor redColor], NSForegroundColorAttributeName,
                                           [UIFont fontWithName:boldFontName size:18.0f], NSFontAttributeName,
                                           [NSValue valueWithCGSize:CGSizeMake(0.0, 0.0)], NSShadowAttributeName,
                                           nil]];
}

【问题讨论】:

  • 您使用了错误的键和值顺序,所以它不起作用。
  • 试试这个:NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor], UITextAttributeTextColor,[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:20.0],UITextAttributeFont, nil]; [[UINavigationBar 外观] setTitleTextAttributes:textTitleOptions];
  • @DhavalBhadania:当我尝试这个时,我收到一个错误,说它们已被弃用......
  • @sharataka 请检查我的答案
  • @sharataka 它在我的应用程序中的 ios 7 中完美运行

标签: ios ios7 uinavigationbar


【解决方案1】:

这里是导航栏标题altering the font style的示例代码sn-ps-

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
test.shadowOffset = CGSizeMake(0, 1);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
                                                       shadow, NSShadowAttributeName,
                                                       [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];

使用图片作为导航栏标题

self.navigationItem.titleView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"self.png"]];

【讨论】:

  • 当我包含这个时,文本颜色没有任何变化:(。
  • 它正在工作,但不在当前视图控制器上。它适用于其他视图控制器。知道如何纠正这个问题吗?
  • @sharataka 请检查链接,我已发送给您,您可能会遗漏一些小东西,请检查一下。我希望它会奏效。它对我有用..
【解决方案2】:
     NSDictionary *titleTextAttributes=@{                                          
                 NSForegroundColorAttributeName: [UIColor redColor],                                                                                                  

    NSFontAttributeName: [UIFont systemFontOfSize:17.0],};

 UINavigationController * objView=(UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;    
    objView.navigationBar.titleTextAttributes = titleTextAttributes;

【讨论】:

  • 我加入了这个,但文字仍然不是红色:(
  • 它不适用于您必须在 rootviewcontroller 中设置的同一个控制器,之后它将起作用
  • UINavigationController * objView=(UINavigationController *)self.window.rootViewController; //self.title="设置"; objView.navigationBar.titleTextAttributes = titleTextAttributes;
  • @sharataka 检查我的更新答案我希望它会工作
  • 你是对的。当我把它放进去时,它适用于其他视图控制器......只是不是我目前使用的那个。如何将它放在 rootViewController 中?我在哪里把代码放在你上面的评论中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多