Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.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 是否全局设置UINavigationBar?_Ios_Swift_Uinavigationbar - Fatal编程技术网

Ios 是否全局设置UINavigationBar?

Ios 是否全局设置UINavigationBar?,ios,swift,uinavigationbar,Ios,Swift,Uinavigationbar,我正在尝试使用下面的代码全局更改UINavigationBar实例,该代码位于didfishlaunchwithoptions(:) 但结果是, 从图中可以看出,颜色是不同的,这意味着相同。(按钮的颜色是我希望导航栏的颜色。) 当我添加代码时,如下所示: navBarApp.isTranslucent = false 结果是 现在颜色是一样的,但是在UINavigationBar和mainView之间有一个奇怪的间隙。那我怎么解决呢?有什么想法吗 编辑: 忘了提到我在使用图书馆了也许这有某

我正在尝试使用下面的代码全局更改
UINavigationBar
实例,该代码位于
didfishlaunchwithoptions(:)

但结果是,

从图中可以看出,颜色是不同的,这意味着相同。(按钮的颜色是我希望导航栏的颜色。)

当我添加代码时,如下所示:

navBarApp.isTranslucent = false
结果是

现在颜色是一样的,但是在
UINavigationBar
mainView
之间有一个奇怪的间隙。那我怎么解决呢?有什么想法吗

编辑:


忘了提到我在使用图书馆了也许这有某种效果

尝试在viewController的
viewDidLoad()
中设置此属性
automaticallyAdjustsScrollViewInsets

override func viewDidLoad() {
    automaticallyAdjustsScrollViewInsets = false
}
或者,最好确保所有控制器中的“调整滚动视图”插图都未勾选

如果在rootViewController中设置,请确保此属性在子控制器中未被覆盖

更新:

上述解决方案应该适用于大多数情况,或者您可以设置背景图像,而不是像@WilsonXJ answer中给出的那样使条形图半透明

您可以使用扩展名

extension UIImage {
   static func imageWithColor(tintColor: UIColor) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
        tintColor.setFill()
        UIRectFill(rect)
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
}
将此用作

navBarApp.setBackgroundImage(UIImage.imageWithColor(tintColor: <Custom color>), for: .default)
navBarApp.setBackgroundImage(UIImage.imageWithColor(tintColor:),用于:。默认值) 试试这个:

let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
UIColor.redColor().setFill()// your color
UIRectFill(rect)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
let navBar = UINavigationBar.appearance()
navBar.setBackgroundImage(image, forBarMetrics: .Default)

它在
viewdiload
Set
automaticallyadjustscrollviewsinsets
to
false
中对我有效。root的
viewdiload
或navigationController的?在root的
viewdiliad
中都试过了,但在root的
viewdiliad
中不起作用。edgesForExtendedLayout=[]是的,我刚试过,可惜没有成功。我还在navigationController中选中了此选项,它已被取消选中。您已在所有控制器中选中,包括UIViewController?我正在使用库()可能这有某种效果?不确定,我没有使用此库。我会告诉我们的。颜色和这个不相配method@Faruk你换了房间吗color@Faruk将redcolor更改为您的颜色,然后重试
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
UIColor.redColor().setFill()// your color
UIRectFill(rect)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
let navBar = UINavigationBar.appearance()
navBar.setBackgroundImage(image, forBarMetrics: .Default)