简体   繁体   English

xcode 7.3 swift访问并更改导航栏中的颜色和文本

[英]xcode 7.3 swift Access and change colors and text in a navigation bar

I'm trying to learn how to access and change theme colors and fonts in one of my view controllers (UIViewController, with a nav bar added manually, no navigation controller is on my storyboard). 我正在尝试学习如何在我的一个视图控制器(UIViewController,手动添加导航栏,情节提要中没有导航控制器)中访问和更改主题颜色和字体。

I've been partially successful with this code in my view controller viewDidLoad: 我在视图控制器viewDidLoad中使用此代码已部分成功:

UINavigationBar.appearance().backgroundColor = theme.TabsColor
UINavigationBar.appearance().barTintColor = theme.NavColor

But this code from another post here on SO compiles/works, but has no effect: 但是此SO上另一篇文章中的代码可以编译/运行,但无效:

self.navigationController?.navigationBar.translucent = false

And I have not been successful at even simple things, like changing the background color of nav bar items, with simple code like this: 而且,即使是简单的事情,我也没有成功,例如使用以下简单代码更改导航栏项目的背景颜色:

for view1 in UINavigationBar.appearance().subviews {
  view1.backgroundColor = theme.CellColor
  for view2 in view1.subviews {
    view2.backgroundColor = theme.CellColor
    for view3 in view2.subviews {
      view3.backgroundColor = theme.CellColor
      for view4 in view3.subviews {
        view4.backgroundColor = theme.CellColor
      }
    }
  }
}

To find and color my StackView items, I give them a restorationIdentifer on the storyboard, then I can find them in my searching loops. 要查找StackView项目并为其着色,请在情节提要上为它们提供restoreIdentifer,然后在搜索循环中找到它们。 But there's no restorationIdentifiers for Nav bars, so that method won't work. 但是没有导航条的restoreIdentifiers,因此该方法无效。

How can I access and turn off .translucent in a nav bar from within my controller? 如何在控制器中的导航栏中访问和关闭.translucent?

How can I access and modify colors and text properties of things on the nav bar that I added to my view controller? 如何在添加到视图控制器的导航栏上访问和修改事物的颜色和文本属性? (I'm using nav bar without navigation controller.) (我使用的是不带导航控制器的导航栏。)

How can I access and modify colors and text on all nav bars (with nav controllers) in my whole app? 如何在整个应用程序中访问和修改所有带有导航控制器的导航栏上的颜色和文本?

Thanks. 谢谢。 I hope that isn't too many questions for one post, but all the questions get at the same thing -- how to access nav bars (with and without controllers) in swift 2.2.. (There are quite a few posts from 2011-2014 on ObjC nav bars, but they haven't been able to help me much. 我希望这不是一个帖子的太多问题,但是所有问题都是一样的-如何在swift 2.2中访问导航栏(带有和不带有控制器)。.(2011年有很多帖子, 2014年在ObjC导航栏上发布,但他们帮不上什么忙。

UPDATE: I have made more progress in my understanding of nav bars. 更新:我对导航栏的理解有了更多的进步。

The most obvious leap (that made me feel like a real newbie, which I am for xcode/swift) was that I could easily get a code reference to my nav bar simply by creating an outlet for it. 最明显的飞跃(这让我感到自己是一个真正的新手,对于xcode / swift来说,我是一个新手)是,我可以轻松地通过为其创建一个出口来轻松获得对导航栏的代码引用。 Doh! h! Then I could reference it directly through the outlet, rather than by using convoluted references involving UINavigationBar or self.navigationController?.navigationBar, etc. 然后,我可以直接通过插座引用它,而不用使用涉及UINavigationBar或self.navigationController?.navigationBar等的复杂引用。

  @IBOutlet weak var NavBar: UINavigationBar!

Following that idea of just using simple outlets, it was also possible to reference the NavBar title in the same way, through an outlet. 遵循只使用简单插座的想法,也可以通过插座以相同的方式引用NavBar标题。

@IBOutlet weak var NavTitle: UINavigationItem!

So this is the kind of code that works for changing NavBar and NavTitle background colors in Swift. 因此,这种代码可用于在Swift中更改NavBar和NavTitle背景颜色。 (Actually, the backgroundColor code below seems useless, and has zero visual effect on my project. It's the barTintColor that actually changes the color of my NavBar.) (实际上,下面的backgroundColor代码似乎无用,并且对我的项目的视觉效果为零。实际上是barTintColor更改了NavBar的颜色。)

    // turning off translucent doesn't help
NavBar.translucent = false
NavBar.backgroundColor = theme.NavColor

// barTintColor actually colors the bar
NavBar.barTintColor = theme.NavColor
NavBar.tintColor = UIColor.redColor() // colors the builtin back arrow

So I can use outlets to reference NavBar and NavTitle (probably NavBarItems too), and can change the background of the NavBar using barTintColor (but not backgroundColor). 因此,我可以使用插座来引用NavBar和NavTitle(也可能是NavBarItems),并且可以使用barTintColor(而不是backgroundColor)来更改NavBar的背景。 I can change the color of the NavItem backItem with NavBar.tintColor. 我可以使用NavBar.tintColor更改NavItem backItem的颜色。

But I still can't figure out how to change the color of the NavTitle text, its font, etc, either by using references like NavBar...whatever or NavTitle...whatever. 但是我还是不知道如何通过使用诸如NavBar ... whatever或NavTitle ... whatever这样的引用来更改NavTitle文本的颜色,其字体等。 I think the answer lies in the titleTextAttributes dictionary somehow, but I can't find code examples on the net or in the Apple (swift) doc. 我认为答案以某种方式位于titleTextAttributes词典中,但是我无法在网上或Apple(速记)文档中找到代码示例。 I tried the code below (cribbed from an ObjC posting here on SO), but it seemed useless (ineffective). 我尝试了下面的代码(摘自SOb在此处的ObjC帖子),但它似乎没有用(无效)。 So I'm still stuck on how to change foreground, background colors for title text, or the font, etc. 因此,我仍然停留在如何更改前景,标题文本的背景颜色或字体等方面。

    //    NavBar.titleTextAttributes - I can't get it to work
NavTitle.title = "Using an outlet helps"
NavBar.titleTextAttributes?.updateValue(UIColor.blueColor(), forKey: NSForegroundColorAttributeName)

I undertook this whole NavBar project because I read somewhere that it was common to use NavBars just to display items and a title, without using them for their navigation smarts or properties. 我承担了整个NavBar项目,因为我在某处读到,通常只使用NavBars来显示项目和标题,而没有将它们用于导航功能或属性是很常见的。

But now after spending a couple of days trying to figure out how they work, I'm just going to abandon them for that purpose. 但是,现在花了几天的时间试图弄清楚它们的工作方式之后,我将为此目的放弃它们。 It's just WAY easier to use a horizontal StackView or a Toolbar instead. 而是使用水平StackView或工具栏更容易。 Hopefully someone can benefit from this long posting one day... 希望有人可以从这一天的漫长发布中受益...

ANOTHER UPDATE: 另一个更新:

I'm beginning to wonder if it's even possible to reference a navigation bar from within a view controller, using syntax that starts with "self.view.xxx." 我开始怀疑是否可以使用以“ self.view.xxx”开头的语法从视图控制器中引用导航栏。 Here's a screenshot of the controller view hierarchy, taken at a debug breakpoint in viewDidLoad. 这是控制器视图层次结构的屏幕快照,是在viewDidLoad中的调试断点处拍摄的。 I can see all the UIViews on the controller page, except for the nav bar. 我可以在控制器页面上看到除导航栏以外的所有UIView。

ViewController的调试层次结构视图,不显示导航栏

So maybe the only way to reference nav bars is (1) with UINavigationBar.xxx to use a global class reference of some kind, or (2) use an IBOutlet, created with the storyboard, to reference the nav bar directly. 因此,引用导航栏的唯一方法可能是(1)使用UINavigationBar.xxx使用某种全局类引用,或(2)使用由情节提要创建的IBOutlet直接引用导航栏。

Here's the answer for Xcode 7.3. 这是Xcode 7.3的答案。 To set the title text color for a navigation bar:- 为导航栏设置标题文本颜色:-

Put this into AppDelegate didFinishLaunchingWithOptions 将此放入AppDelegate didFinishLaunchingWithOptions

    if let _ = UINavigationBar.appearance().titleTextAttributes {
        print("setting textAttributes")
        UINavigationBar.appearance().titleTextAttributes?.updateValue(UIColor.whiteColor(), forKey: "TextColor")
    }
    else {
        print("textAttributes is nil")
        UINavigationBar.appearance().titleTextAttributes = ["TextColor" : UIColor.whiteColor()]
    }

I think you want the navigation bar to have color in order to do that you need to have Navigation Bar in the bottom left selected and in the top right you can change the bar tint which changes the Navigation Bar's color. 我认为您要使导航栏具有颜色才能做到这一点,您需要在左下方选择“导航栏”,并在右上角更改“色调”,以更改导航栏的颜色。 Refer to the picture. 请参考图片。 http://i.stack.imgur.com/oRBpo.png http://i.stack.imgur.com/oRBpo.png

The answer is embedded in that long post above. 答案嵌入上面的那篇长文章中。 For clarity and simplicity, I'll summarize it here. 为了清楚和简单起见,我将在这里进行总结。

To programmatically change NavBar colors, you can reference the NavBar in two ways: by using an explicit outlet in your view controller code to reference the nav bar, the nav title, or nav items. 要以编程方式更改NavBar的颜色,可以通过两种方式引用NavBar:通过在视图控制器代码中使用显式出口来引用导航栏,导航标题或导航项。 Or you can reference the global Nav bar with UINavigationBar.xxx. 或者,您可以使用UINavigationBar.xxx引用全局导航栏。

But since the NavBar is not part of the object hierarchy below self.view, I was not able to create a self.view.xxx reference that worked. 但是由于NavBar不在self.view下面的对象层次结构中,因此我无法创建有效的self.view.xxx引用。

I still don't know how to reference the text font, colors, size of the NavTitle. 我仍然不知道如何引用NavTitle的文本字体,颜色,大小。 This is an issue if you pick a dark Nav Bar color and want to use white text on dark background (tint). 如果您选择了深色的导航栏颜色,并想在深色背景上使用白色文本(色调),则会出现此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM