美文网首页
设置导航栏的背景色——2018-07-04

设置导航栏的背景色——2018-07-04

作者: 景彧 | 来源:发表于2019-10-08 17:50 被阅读0次

*设置导航栏背景样式有两个方法,setBackgroundImage:forBarMetrics:setBarTintColor:推荐使用前者。
理由是:
在 iOS 8.2 或者之前的版本,如果导航栏的 translucent值为true时,用 barTintColor 去设置导航栏的背景样式,然后改变barTintColor 的颜色,那么当边缘左滑返回手势取消的时候导航栏的背景色会闪烁。
为了避免这种情况发生,推荐用setBackgroundImage:forBarMetrics: 来改变导航栏的背景样式。

*translucent 这个属性的值在初始化后尽量不要随意修改,否则容易发生界面的布局错乱。

*当translucent为 true 时,可以用以下方法使导航栏背景透明:

navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
navigationController?.navigationBar.shadowImage = UIImage() // shadowImage 就是那根 1px 的细线

当需要改变导航栏背景色的透明度时,可以改变 setBackgroundImage:forBarMetrics:中 image 的 alpha 值。

如果需要显示或隐藏导航栏,一般只需要在viewWillAppear:里设置,代码如下:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    navigationController?.setNavigationBarHidden(hidden, animated: animated)
}

最好不要在 viewWillDisappear:或者其他发生跳转的方法里去设置,这样不容易管理。还是那句宗旨,你只需要关心当前 view controller导航栏的样式。

当然最好还是不要隐藏导航栏,因为和系统边缘右滑返回手势一起使用可能会触发一些苹果的 bug。

相关文章

网友评论

      本文标题:设置导航栏的背景色——2018-07-04

      本文链接:https://www.haomeiwen.com/subject/hrqyuftx.html