我想把导航栏的背景设置为黑色,里面的所有颜色都是白色。

所以,我使用了这段代码:

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

但后退按钮的文本颜色,箭头和栏按钮仍然默认为蓝色。 如何改变这些颜色像下面的图像?


当前回答

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

其他回答

范的回答对我很管用。下面是c#开发者使用Xamarin.iOS/MonoTouch的相同解决方案:

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;

Swift 5/iOS 13

更改控制器标题的颜色:

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

Swift / iOS8

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

iOS设置中的辅助功能控件似乎覆盖了你在导航栏按钮上尝试的所有颜色。确保你所有的设置都是默认位置(设置增加对比度,粗体文本,按钮形状等关闭),否则你不会看到任何变化。一旦我这样做了,所有的颜色变化代码开始工作,如预期的。你可能不需要把它们都关掉,但我没有进一步探讨。

如果你想改变标题文本大小和文本颜色,你必须改变NSDictionary titleTextAttributes,它的两个对象:

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