首页 文章

iPhone导航栏 Headers 文字颜色

提问于
浏览
278

默认情况下,iOS导航栏 Headers 颜色似乎是白色 . 有没有办法将它改成不同的颜色?

我知道使用图像的 navigationItem.titleView 方法 . 由于我的设计技巧有限,而且我没有获得标准光面,我更喜欢更改文字颜色 .

任何见解都会非常感激 .

30 回答

  • 6

    现代方法

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

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

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

    经典方法

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

    您需要使用 UILabel 作为 navigationItemtitleView .

    标签应该:

    • 具有清晰的背景色( label.backgroundColor = [UIColor clearColor] ) .

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

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

    • 您还需要将文本对齐设置为居中( label.textAlignment = NSTextAlignmentCenter (旧版SDK的 UITextAlignmentCenter ) .

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

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

    如果要验证这一点,请将此代码放入 PageThreeViewController.m PageThreeViewController.mApple's NavBar sample . 这将用黄色标签替换文本 . 除了颜色之外,这应该与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的答案如下 . 我的代码显示了效果,但是他的代码提供了一种更简单的方法来将其放在现有视图控制器上 .

  • 418

    简短又甜蜜 .

    [[[self navigationController] navigationBar] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];
    
  • 8

    从iOS 5开始,我们必须使用titleTextAttribute Dictionary(UInavigation控制器类引用中的预定义字典)设置 Headers 文本颜色和导航栏的字体 .

    [[UINavigationBar appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [UIColor blackColor],UITextAttributeTextColor, 
    [UIFont fontWithName:@"ArialMT" size:16.0], UITextAttributeFont,nil]];
    
  • 38

    为了使问题保持最新,我将添加 Alex R. R. 解决方案,但在Swift中:

    self.navigationController.navigationBar.barTintColor = .blueColor()
    self.navigationController.navigationBar.tintColor = .whiteColor()
    self.navigationController.navigationBar.titleTextAttributes = [
        NSForegroundColorAttributeName : UIColor.whiteColor()
    ]
    

    结果如下:

    enter image description here

  • 127

    Method 1 ,在IB中设置:

    enter image description here

    Method 2 ,一行代码:

    navigationController?.navigationBar.barTintColor = UIColor.blackColor()
    
  • 21

    你应该调用[label sizeToFit];设置文本以防止在其他按钮占据导航栏时 Headers 视图中自动重新定位标签时的奇怪偏移 .

  • 2

    我的导航按钮将文本从中心抛出(当你只有一个按钮时)我遇到了问题 . 为了解决这个问题,我只是改变了我的帧大小:

    CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);
    
  • 13

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

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

    如果您尝试更改页面上的颜色,tewha的解决方案效果很好,但我希望能够更改每个页面上的颜色 . 我做了一些小的修改,以便它适用于 UINavigationControllerUINavigationController

    NavigationDelegate.h

    //This will change the color of the navigation bar
    #import <Foundation/Foundation.h>
    @interface NavigationDelegate : NSObject<UINavigationControllerDelegate> {
    }
    @end
    

    NavigationDelegate.m

    #import "NavigationDelegate.h"
    @implementation NavigationDelegate
    
    - (void)navigationController:(UINavigationController *)navigationController 
          willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
        CGRect frame = CGRectMake(0, 0, 200, 44);//TODO: Can we get the size of the text?
        UILabel* label = [[[UILabel alloc] initWithFrame:frame] autorelease];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:20.0];
        label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        label.textAlignment = UITextAlignmentCenter;
        label.textColor = [UIColor yellowColor];
        //The two lines below are the only ones that have changed
        label.text=viewController.title;
        viewController.navigationItem.titleView = label;
    }
    @end
    
  • 9

    遇到与我们在navBar中插入按钮时移动的标签相同的问题(如同其他)(在我的情况下,我有一个微调器,我在加载日期时用按钮替换),上述解决方案不起作用对我来说,所以这里是有效的,并始终将标签保持在同一个地方:

    - (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
        //CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);
       CGRect frame = CGRectMake(0, 0, 180, 44);
        UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
    
    
    
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:20.0];
        label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        label.textAlignment = UITextAlignmentCenter;
        label.textColor = [UIColor yellowColor];
        self.navigationItem.titleView = label;
        label.text = NSLocalizedString(@"Latest Questions", @"");
        [label sizeToFit];
    }
    
    return self;
    
  • 1

    我确实认为设置 UINavigationBar 的颜色的正确方法是:

    NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],UITextAttributeTextColor, nil];
    self.titleTextAttributes = attributes;
    

    上面的代码是 UINavigationBar 上的子类,显然无需子类化 .

  • 6

    建议设置self.title,因为在推送子导航栏或在tabbars上显示 Headers 时使用它 .

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // create and customize title view
            self.title = NSLocalizedString(@"My Custom Title", @"");
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
            titleLabel.text = self.title;
            titleLabel.font = [UIFont boldSystemFontOfSize:16];
            titleLabel.backgroundColor = [UIColor clearColor];
            titleLabel.textColor = [UIColor whiteColor];
            [titleLabel sizeToFit];
            self.navigationItem.titleView = titleLabel;
            [titleLabel release];
        }
    }
    
  • 14

    Swift 版本

    我发现大多数人都提出了Objective_C版本的答案

    我想通过使用Swift为任何需要它的人实现这个功能 .

    In ViewDidload

    1.使NavigationBar背景变为颜色(例如:BLUE)

    self.navigationController?.navigationBar.barTintColor = UIColor.blueColor()
    

    2.使NavigationBar背景成为图像(例如:ABC.png)

    let barMetrix = UIBarMetrics(rawValue: 0)!
    
    self.navigationController?.navigationBar
          .setBackgroundImage(UIImage(named: "ABC"), forBarMetrics: barMetrix)
    

    3.更改NavigationBar Headers (例如:[字体:Futura,10] [颜色:红色])

    navigationController?.navigationBar.titleTextAttributes = [
                NSForegroundColorAttributeName : UIColor.redColor(),
                NSFontAttributeName : UIFont(name: "Futura", size: 10)!
            ]
    

    (HINT1:不要忘了UIFont后标记“!”)(HINT2:有很多的 Headers 文本的属性,命令点击“NSFontAttributeName”即可进入类和视图键名,以及他们所需要的对象类型)

    I hope I can help!:D

  • 2

    在IOS 7和8中,您可以更改 Headers 的颜色,让我们说绿色

    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor greenColor] forKey:NSForegroundColorAttributeName];
    
  • 3

    像这样使用Orientation支持

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,40)];
    [view setBackgroundColor:[UIColor clearColor]];
    [view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ];
    
    UILabel *nameLabel = [[UILabel alloc] init];
    [nameLabel setFrame:CGRectMake(0, 0, 320, 40)];
    [nameLabel setBackgroundColor:[UIColor clearColor]];
    [nameLabel setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin |UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin];
    [nameLabel setTextColor:[UIColor whiteColor]];
    [nameLabel setFont:[UIFont boldSystemFontOfSize:17]];
    [nameLabel setText:titleString];
    [nameLabel setTextAlignment:UITextAlignmentCenter];
    [view addSubview:nameLabel];
    [nameLabel release];
    self.navigationItem.titleView = view;
    [view release];
    
  • 10

    我已经自定义了navigationBar的背景图像和左按钮项,灰色 Headers 不适合背景 . 然后我用:

    [self.navigationBar setTintColor:[UIColor darkGrayColor]];
    

    将色调颜色更改为灰色 . 现在 Headers 是白色的!这就是我想要的 .

    希望也能帮助:)

  • 226

    Swift 4版本:

    self.navigationController.navigationBar.titleTextAttributes = [
            NSAttributedStringKey.foregroundColor: UIColor.green]
    
  • 3

    我知道这是一个很老的线程,但我认为知道iOS 5的新用户会很有用为 Build Headers 属性带来了新属性 .

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

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

    例如:

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

    设置 Headers 的字体大小我使用了以下条件..也许对任何人都有帮助

    if ([currentTitle length]>24) msize = 10.0f;
        else if ([currentTitle length]>16) msize = 14.0f;
        else if ([currentTitle length]>12) msize = 18.0f;
    
  • 2

    这适用于Swift:

    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
    
  • 6

    这是缺少的事情之一 . 您最好的选择是创建自己的自定义导航栏,添加文本框,并以这种方式操纵颜色 .

  • 10

    根据Steven Fisher的回答,我写了这段代码:

    - (void)setTitle:(NSString *)title
    {
        [super setTitle:title];
        UILabel *titleView = (UILabel *)self.navigationItem.titleView;
        if (!titleView) {
            titleView = [[UILabel alloc] initWithFrame:CGRectZero];
            titleView.backgroundColor = [UIColor clearColor];
            titleView.font = [UIFont boldSystemFontOfSize:20.0];
            titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    
            titleView.textColor = [UIColor yellowColor]; // Change to desired color
    
            self.navigationItem.titleView = titleView;
            [titleView release];
        }
        titleView.text = title;
        [titleView sizeToFit];
    }
    

    除了正确处理框架之外,此代码的优点是,如果更改控制器的 Headers ,自定义 Headers 视图也将更新 . 无需手动更新 .

    另一个很大的优点是它可以非常简单地启用自定义 Headers 颜色 . 您需要做的就是将此方法添加到控制器 .

  • 2

    这是我基于史蒂文斯的解决方案

    唯一真正的区别是我把一些处理用于调整位置,如果取决于文本长度,似乎类似于苹果如何做到这一点

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(([self.title length] < 10 ? UITextAlignmentCenter : UITextAlignmentLeft), 0, 480,44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont boldSystemFontOfSize: 20.0f];
    titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    titleLabel.textAlignment = ([self.title length] < 10 ? UITextAlignmentCenter : UITextAlignmentLeft);
    titleLabel.textColor = [UIColor redColor];
    titleLabel.text = self.title;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    

    您可能需要根据字体大小调整10值

  • 13

    可以在appdelegate文件中使用此方法,并且可以在每个视图中使用

    +(UILabel *) navigationTitleLable:(NSString *)title
    {
    CGRect frame = CGRectMake(0, 0, 165, 44);
    UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
    label.backgroundColor = [UIColor clearColor];
    label.font = NAVIGATION_TITLE_LABLE_SIZE;
    label.shadowColor = [UIColor whiteColor];
    label.numberOfLines = 2;
    label.lineBreakMode = UILineBreakModeTailTruncation;    
    label.textAlignment = UITextAlignmentCenter;
    [label setShadowOffset:CGSizeMake(0,1)]; 
    label.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
    
    //label.text = NSLocalizedString(title, @"");
    
    return label;
    }
    
  • 6

    这是一个非常古老的线程,但我想为iOS 7及更高版本提供设置导航栏 Headers 的颜色,大小和垂直位置的答案

    For Color and Size

    NSDictionary *titleAttributes =@{
                                    NSFontAttributeName :[UIFont fontWithName:@"Helvetica-Bold" size:14.0],
                                    NSForegroundColorAttributeName : [UIColor whiteColor]
                                    };
    

    For Vertical Position

    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-10.0 forBarMetrics:UIBarMetricsDefault];
    

    Set Title and assign the attributes dictionary

    [[self navigationItem] setTitle:@"CLUBHOUSE"];
    self.navigationController.navigationBar.titleTextAttributes = titleAttributes;
    
  • 180

    在任何视图控制器viewDidLoad或viewWillAppear方法中使用以下代码 .

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        //I am using UIColor yellowColor for an example but you can use whatever color you like   
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor yellowColor]};
    
        //change the title here to whatever you like
        self.title = @"Home";
        // Do any additional setup after loading the view.
    }
    
  • 1

    titleTextAttributes显示条形 Headers 文本的属性 .

    @property(非原子,复制)NSDictionary * titleTextAttributes讨论您可以使用NSString UIKit Additions Reference中描述的文本属性键为文本属性字典中的 Headers 指定字体,文本颜色,文本阴影颜色和文本阴影偏移 .

    可用性适用于iOS 5.0及更高版本 . 在UINavigationBar.h中声明

  • 5
    self.navigationItem.title=@"Extras";
    [self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaNeue" size:21], NSFontAttributeName,[UIColor whiteColor],UITextAttributeTextColor,nil]];
    
  • 3

    使用新的iOS 7文本属性和现代目标c更新Alex R. R.的帖子以减少噪音:

    NSShadow *titleShadow = [[NSShadow alloc] init];
    titleShadow.shadowColor = [UIColor blackColor];
    titleShadow.shadowOffset = CGSizeMake(-1, 0);
    NSDictionary *navbarTitleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],
                                                NSShadowAttributeName:titleShadow};
    
    [[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
    
  • 40

    以上大部分建议现已弃用,iOS 7使用 -

    NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
                                   [UIColor whiteColor],NSForegroundColorAttributeName, 
                                   [UIColor whiteColor],NSBackgroundColorAttributeName,nil];
    
    self.navigationController.navigationBar.titleTextAttributes = textAttributes;
    self.title = @"Title of the Page";
    

    另外,检查NSAttributedString.h以获取可以设置的各种文本属性 .

相关问题