gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:06:21 25 4
gpt4 key购买 nike

我正在尝试学习如何在一个视图控制器(UIViewController,手动添加导航栏,情节提要板上没有导航控制器)中访问和更改主题颜色和字体。

我在视图控制器viewDidLoad中使用此代码已部分成功:

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


但是此SO上另一篇文章中的代码可以编译/运行,但无效:

self.navigationController?.navigationBar.translucent = false


而且,即使是简单的事情,我也没有成功,例如使用以下简单代码更改导航栏项目的背景颜色:

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
}
}
}
}


要查找StackView项目并为其着色,请在情节提要上为它们提供restoreIdentifer,然后在搜索循环中找到它们。但是没有导航条的restoreIdentifiers,因此该方法不起作用。

如何在控制器中的导航栏中访问和关闭.translucent?

如何在添加到视图控制器的导航栏上访问和修改事物的颜色和文本属性? (我正在使用没有导航控制器的导航栏。)

如何在整个应用程序中访问和修改所有导航栏上的颜色和文本(使用导航控制器)?

谢谢。我希望这不是一个帖子的太多问题,但是所有问题都可以解决-在swift 2.2中如何访问导航栏(带和不带控制器)。.(2011年有很多帖子, 2014年在ObjC导航栏上发布,但他们帮不上什么忙。

更新:我对导航栏的理解有了更多的进步。

最明显的飞跃(这让我觉得自己像是一个真正的新手,对于xcode / swift来说,我是一个新手)是,只要为它的导航栏创建一个出口,就可以轻松地获得对我的导航栏的代码引用。 h!然后,我可以直接通过插座引用它,而不用使用涉及UINavigationBar或self.navigationController?.navigationBar等的复杂引用。

  @IBOutlet weak var NavBar: UINavigationBar!


遵循只使用简单插座的想法,也可以通过插座以相同的方式引用NavBar标题。

@IBOutlet weak var NavTitle: UINavigationItem!


因此,这种代码可用于在Swift中更改NavBar和NavTitle背景颜色。 (实际上,下面的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


因此,我可以使用插座来引用NavBar和NavTitle(也可能是NavBarItems),并且可以使用barTintColor(而不是backgroundColor)更改NavBar的背景。我可以使用NavBar.tintColor更改NavItem backItem的颜色。

但是我仍然不知道如何通过使用诸如NavBar ... whatever或NavTitle ... whatever这样的引用来更改NavTitle文本的颜色,其字体等。我认为答案以某种方式位于titleTextAttributes字典中,但是我无法在网上或Apple(速记)文档中找到代码示例。我尝试了下面的代码(摘自SOb在此处的ObjC帖子),但它似乎没有用(无效)。所以我仍然停留在如何更改前景,标题文本的背景颜色或字体等方面。

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


我进行了整个NavBar项目,因为我在某处读到,使用NavBars只是为了显示项目和标题,而没有将它们用于导航功能或属性是很常见的。

但是现在花了几天的时间试图弄清楚它们的工作方式之后,我将为此目的放弃它们。而是使用水平StackView或工具栏更容易。希望有人可以从这一天的漫长发布中受益...

另一个更新:

我开始怀疑是否可以使用以“ self.view.xxx”开头的语法从视图控制器中引用导航栏。这是控制器视图层次结构的屏幕快照,是在viewDidLoad中的调试断点处拍摄的。我可以在控制器页面上看到除导航栏以外的所有UIView。

Debug hierarchy view of viewcontroller, showing no Nav Bar

因此,引用导航栏的唯一方法可能是(1)使用UINavigationBar.xxx使用某种全局类引用,或(2)使用由情节提要创建的IBOutlet直接引用导航栏。

最佳答案

这是Xcode 7.3的答案。为导航栏设置标题文本颜色:-

将此放入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()]
}

关于ios - xcode 7.3 swift访问并更改导航栏中的颜色和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36318133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com