Iphone IOS 7导航栏文本和箭头颜色

Iphone IOS 7导航栏文本和箭头颜色,iphone,ios,objective-c,uinavigationbar,ios7,Iphone,Ios,Objective C,Uinavigationbar,Ios7,我想将导航栏的背景设置为黑色,其中的所有颜色设置为白色 因此,我使用了以下代码: [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, [UIColor whiteColor], NSFore

我想将导航栏的背景设置为黑色,其中的所有颜色设置为白色

因此,我使用了以下代码:

[[UINavigationBar appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [UIColor whiteColor],
      NSForegroundColorAttributeName,
      [UIColor whiteColor],
      NSForegroundColorAttributeName,
      [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
      NSForegroundColorAttributeName,
      [UIFont fontWithName:@"Arial-Bold" size:0.0],
      NSFontAttributeName,
      nil]];
但是后退按钮文本颜色箭头条形按钮仍然默认为蓝色
如何更改这些颜色,如下图所示?


来自
UINavigationBar
的某些属性的行为已从iOS 7更改。您可以在下图中看到:


两个美丽的链接我想与你分享。有关更多详细信息,请访问以下链接:


  • 苹果公司文档中说:

    默认情况下,此颜色为半透明,除非设置 将半透明属性设置为NO

    示例代码:

    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar 
     setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    

    这一次我花了半天的时间才弄明白,但这对我来说是有效的。 在初始化navigationController的rootViewController中,我将以下代码放在我的ViewDidDisplay方法中:

    //set bar color
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:85.0/255.0 green:143.0/255.0 blue:220.0/255.0 alpha:1.0]];
    //optional, i don't want my bar to be translucent
    [self.navigationController.navigationBar setTranslucent:NO];
    //set title and title color
    [self.navigationItem setTitle:@"Title"];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]];
    //set back button color
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
    //set back button arrow color
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    

    我在这篇

    上的完整帖子对我来说非常有用。对于使用Xamarin.iOS/MonoTouch的C#开发人员,以下是相同的解决方案:

    var navigationBar = NavigationController.NavigationBar; //or another reference
    navigationBar.BarTintColor = UIColor.Blue;
    navigationBar.TintColor = UIColor.White;
    navigationBar.SetTitleTextAttributes(new UITextAttributes() { TextColor = UIColor.White });
    navigationBar.Translucent = false;
    

    要以正确的方式更改
    UINavigationBar
    标题的颜色,请使用此代码:

    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]];
    

    uitexttributetextcolor
    在最新的ios 7版本中不推荐使用。使用
    NSForegroundColorAttributeName

    我认为前面的答案是正确的,这是做同样事情的另一种方法。我在这里与其他人分享它,以防它对某人有用。以下是如何在ios7中更改导航栏的文本/标题颜色:

    UIColor *red = [UIColor colorWithRed:254.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0];
    NSMutableDictionary *navBarTextAttributes = [NSMutableDictionary dictionaryWithCapacity:1];
    [navBarTextAttributes setObject:red forKey:NSForegroundColorAttributeName ];
    self.navigationController.navigationBar.titleTextAttributes = navBarTextAttributes;
    

    Swift/iOS8

    let textAttributes = NSMutableDictionary(capacity:1)
    textAttributes.setObject(UIColor.whiteColor(), forKey: NSForegroundColorAttributeName)
    navigationController?.navigationBar.titleTextAttributes = textAttributes
    

    看起来,
    iOS设置中的
    Accessibility
    控件几乎覆盖了您尝试在导航栏按钮上按颜色执行的所有操作。确保将所有设置设置为默认位置(将“增加对比度”、“粗体文本”、“按钮形状”等设置为“关闭”),否则将看不到任何更改。一旦我这样做了,所有的颜色变化代码开始工作的预期。您可能不需要将它们全部关闭,但我没有进一步研究。

    如果您希望更改标题文本大小和文本颜色,则必须更改NSDictionary titleTextAttributes,用于其2个对象:

        self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:13.0],NSFontAttributeName,
                                                                      [UIColor whiteColor], NSForegroundColorAttributeName, 
                                                                      nil];
    

    Swift3、ios 10

    要全局分配颜色,请在AppDelegate
    didFinishLaunchingWithOptions
    中:

    let textAttributes = [NSForegroundColorAttributeName:UIColor.white]
    UINavigationBar.appearance().titleTextAttributes = textAttributes
    
    Swift 4,iOS 11

    let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
    UINavigationBar.appearance().titleTextAttributes = textAttributes
    

    Swift 5/iOS 13

    要更改控制器中标题的颜色,请执行以下操作:

    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    

    如果([self.window respondsToSelector:@selector(setTintColor:)])self.window.tintColor=[UIColor whiteColor];请在AppDelegate中尝试;有趣的是,您的代码对我来说非常有用(只是想将文本颜色更改为白色)。谢谢你用的蓝色是什么?看起来不错@ebi有些人不喜欢分享。只需使用颜色选择器即可将其挑选出来。或者……给你:RGB:(18,60,133)我已经试过了:self.nav.navigationBar.tintColor=[UIColor-whiteColor];self.nav.navigationBar.barTintColor=[uicolorWithred:6.0/255.0绿色:12.0/255.0蓝色:19.0/255.0 alpha:1.0];但所有的东西都是蓝色的。我以前读过,但这次只改变了文本,而不是箭头。它仍然是蓝色的:(@1110:看一看:@Chelsea:这行怎么样?:
    [self.navigationController.navigationBar setTitleTextAttributes:@{[UIFontWithName:@“YOURFONT”size:14],NSFontAttributeName}];
    别忘了你可以在appDelegate中更改整个应用程序的色调:[[UINavigationBar外观]setTintColor:[UIColor blackColor]];如果您在更改后向箭头的颜色时遇到问题,此代码段将解决此问题。至少,您需要将最后一行代码添加到实际的rootViewController中。是的,John。我昨天也为此浪费了大约一个小时。一个问题(大问题)是因为文档指出了所有可能的属性太糟糕了。知道它们可能被列在哪里吗?泰。谢谢@john它与我的自定义颜色重叠,你能解释一下为什么吗?我想你应该提出一个新问题。我无法告诉你为什么,除非我看到你的代码和你在做什么。@MacGeekThanks,它起作用了:)。但是iOS 7中不推荐使用UITextAttributeTextColor,现在您应该使用NSForeGroundColorAttributeNameId,因为您不知道您可以像以前那样设置SetTitleTextAttributes。让textAttributes=NSMutableDictionary(容量:1)现在是NSMutableDictionary初始化的正确调用。我喜欢这种方法,因为您可以在AppDelegate本身中设置它,并且它使用外观代理。这正是发生在我身上的情况。启用“粗体文本”时,我无法更改条形按钮项的着色颜色。这不好。现在我必须使用图形来代替:-(
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]