为了账号安全,请及时绑定邮箱和手机立即绑定

iPhone导航栏标题文字颜色

iPhone导航栏标题文字颜色

森栏 2019-08-14 16:54:31
iPhone导航栏标题文字颜色默认情况下,iOS导航栏标题颜色似乎是白色。有没有办法将其改为不同的颜色?我知道navigationItem.titleView使用图像的方法。由于我的设计技巧有限,而且我没有获得标准光面,我更喜欢更改文字颜色。任何见解都会非常感激。
查看完整描述

3 回答

?
DIEA

TA贡献1820条经验 获得超2个赞

现代的方法

现代方式,对于整个导航控制器......在加载导航控制器的根视图时执行此操作一次。

[self.navigationController.navigationBar setTitleTextAttributes:
   @{NSForegroundColorAttributeName:[UIColor yellowColor]}];

但是,这似乎对后续视图没有影响。

经典的方法

旧的方式,每个视图控制器(这些常量适用于iOS 6,但如果想在iOS 7外观上按照视图控制器进行操作,您将需要相同的方法但具有不同的常量):

您需要使用一个UILabel作为titleViewnavigationItem

标签应该:

  • 有明确的背景颜色(label.backgroundColor = [UIColor clearColor])。

  • 使用粗体20pt系统字体(label.font = [UIFont boldSystemFontOfSize: 20.0f])。

  • 有50%alpha(label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5])的黑色阴影。

  • 您还需要将文本对齐设置为居中(label.textAlignment = NSTextAlignmentCenterUITextAlignmentCenter对于较旧的SDK)。

将标签文本颜色设置为您想要的任何自定义颜色。您确实需要一种不会导致文本混合成阴影的颜色,这种颜色难以阅读。

我通过反复试验解决了这个问题,但我想出的价值最终太简单了,以至于他们不能成为苹果选择的。:)

如果你想验证这一点,放弃这一代码到initWithNibName:bundle:PageThreeViewController.m苹果的NavBar样品。这将用黄色标签替换文本。除了颜色之外,这应该与Apple代码生成的原始文件无法区分。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // this will appear as the title in the navigation bar
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:20.0];
        label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        label.textAlignment = NSTextAlignmentCenter;
                           // ^-Use UITextAlignmentCenter for older SDKs.
        label.textColor = [UIColor yellowColor]; // change this color
        self.navigationItem.titleView = label;
        label.text = NSLocalizedString(@"PageThreeTitle", @"");
        [label sizeToFit];
    }

    return self;}

编辑:另外,阅读Erik B的答案如下。我的代码显示了效果,但是他的代码提供了一种更简单的方法来将其放在现有视图控制器上。


查看完整回答
反对 回复 2019-08-14
?
隔江千里

TA贡献1906条经验 获得超10个赞

我知道这是一个非常古老的线程,但我认为知道新用户iOS 5带来了一个用于建立标题属性的新属性会很有用。

您可以使用UINavigationBar setTitleTextAttributes来设置字体,颜色,偏移和阴影颜色。

此外,您可以为UINavigationBars整个应用程序中的所有设置相同的默认UINavigationBar标题文本属性。

例如:

NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIColor whiteColor],UITextAttributeTextColor, 
                                            [UIColor blackColor], UITextAttributeTextShadowColor, 
                                            [NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];


查看完整回答
反对 回复 2019-08-14
?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

在iOS 5中,您可以通过以下方式更改navigationBar标题颜色:

navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor yellowColor]};


查看完整回答
反对 回复 2019-08-14
  • 3 回答
  • 0 关注
  • 931 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信