Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中更改导航栏的颜色_Ios_Swift_Uinavigationbar_Tintcolor_Bartintcolor - Fatal编程技术网

Ios 在Swift中更改导航栏的颜色

Ios 在Swift中更改导航栏的颜色,ios,swift,uinavigationbar,tintcolor,bartintcolor,Ios,Swift,Uinavigationbar,Tintcolor,Bartintcolor,我的ViewController中有一个MKMap,我允许我的用户在“标准”、“混合”和“卫星”之间切换地图类型,就像在地图应用程序中一样。就像在地图应用程序中一样,我有一个底部栏,然后在顶部有一个导航栏。当用户在地图类型之间切换时,条形图应变为黑色背景,白色按钮表示“混合”和“卫星”,白色背景,蓝色按钮表示“标准” 我可以正确更改底部栏的颜色,但根本无法更改导航栏。以下是我在用户更改地图类型时的功能: func changeMapType(sender:UISegmentedControl){

我的ViewController中有一个MKMap,我允许我的用户在“标准”、“混合”和“卫星”之间切换地图类型,就像在地图应用程序中一样。就像在地图应用程序中一样,我有一个底部栏,然后在顶部有一个导航栏。当用户在地图类型之间切换时,条形图应变为黑色背景,白色按钮表示“混合”和“卫星”,白色背景,蓝色按钮表示“标准”

我可以正确更改底部栏的颜色,但根本无法更改导航栏。以下是我在用户更改地图类型时的功能:

func changeMapType(sender:UISegmentedControl){
    if mapType.selectedSegmentIndex == 0{
        mapView.mapType = MKMapType.Standard
        toolbar.barTintColor = UIColor(red:1.0, green:1.0, blue:1.0, alpha:1.0)
        toolbar.tintColor = UIColor(red:0.0, green:122.0/255.0, blue:1.0, alpha:1.0)
        self.navigationController?.navigationBar.translucent = false
        self.navigationController?.navigationBar.barTintColor = UIColor(red:1.0, green:1.0, blue:1.0, alpha:1.0)
        self.navigationController?.navigationBar.tintColor = UIColor(red:0.0, green:122.0/255.0, blue:1.0, alpha:1.0)

        defaults.setValue("Standard", forKey: "initialMapType")
    }
    else if mapType.selectedSegmentIndex == 1{
        mapView.mapType = MKMapType.Hybrid
        toolbar.barTintColor = UIColor.blackColor()
        toolbar.tintColor = UIColor.whiteColor()
        self.navigationController?.navigationBar.translucent = false
        self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
        self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()

    defaults.setValue("Hybrid", forKey: "initialMapType")

}
else if mapType.selectedSegmentIndex == 2{
    mapView.mapType = MKMapType.Satellite
    toolbar.barTintColor = UIColor.blackColor()
    toolbar.tintColor = UIColor.whiteColor()
    self.navigationController?.navigationBar.translucent = false
    self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
    self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()

    defaults.setValue("Satellite", forKey: "initialMapType")
}
}


有人知道我必须做些什么才能让导航栏改变颜色吗?

这很可能是因为您的self.navigationController是空的。请先尝试一下!这是一个非常常见的问题。可能您从错误的视图控制器调用它,而实际上它没有NC。您可以使用.appearance()来解决这个问题接口,如下所示:

要更改栏的着色颜色(导航栏的背景),请执行以下操作:

要更改文本的颜色,请执行以下操作:

UINavigationBar.appearance().titleTextAttributes = [UITextAttributeTextColor: UIColor.whiteColor()]
请注意,这会更改整个应用程序的导航栏(基本上,外观是更改默认设置的方式),因此可能不合适

希望有帮助


编辑:检查导航控制器是否为空的简单方法是强制展开变量self.navigationController!.navigationBar而不是?,如果你的应用程序崩溃,你的问题就在这里(但不要告诉我建议的任何人,因为它不是很灵巧的解决方案,尽管速度很快)为Swift 3、4、4.2、5+更新

UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
//设置导航栏

UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Swift 4

UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Swift 4.2、5+

UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false

也可以在这里检查:

你说你有一个导航栏。但你实际上是在导航控制器内吗?是的,我在导航控制器内。像这样试试好吧,事实上,现在你说我回去检查了,结果发现我不知怎的在另一个导航控制器内有一个导航控制器,这就是c因为这个问题。我删除了它,我的导航栏现在工作得很好。我觉得很傻,我被困在上面了一段时间。非常感谢你的帮助!是的,这是我的NC的一个问题。不知怎的,我在另一个导航控制器中有一个导航控制器,这就是导致我的问题的原因。我删除了它,它现在工作得很好。谢谢非常感谢你的帮助!