我有一个应用程序,有时需要它的导航栏融入内容。
有人知道怎么去掉或者改变这个烦人的小条的颜色吗?
在下图中,我说的是根视图控制器下面1px的高度线
我有一个应用程序,有时需要它的导航栏融入内容。
有人知道怎么去掉或者改变这个烦人的小条的颜色吗?
在下图中,我说的是根视图控制器下面1px的高度线
当前回答
这里有一个非常重要的注意事项——改变UIViewController的navigationItem的外观比直接改变navigationBar要灵活得多。
为什么这么问?
For the simple reason that the navigationItem is tied to a single UIViewController and represents the state of the navigationBar for that particular UIViewController. This is big, as you don't have to handle the navigation bar changes between different view controllers within viewWillAppear (or something similar), as you would if you mutated the navigationBar; which is, remember, shared between all view controllers of a given navigation stack (UINavigationController), and changing it in one place changes it for all view controllers up to the stack.
你只要为一个特定的视图控制器设置正确的UINavigationBarAppearance, UIKit就会正确地更新导航栏样式,这取决于当前哪个视图控制器是导航堆栈上的顶视图控制器。
navigationItem.standardAppearance` = `UINavigationBarAppearance()
其他回答
想把Serhii回答的Swift版本加进去。我用下面的代码创建了一个uibareextension .swift:
import Foundation
import UIKit
extension UINavigationBar {
func hideBottomHairline() {
self.hairlineImageView?.isHidden = true
}
func showBottomHairline() {
self.hairlineImageView?.isHidden = false
}
}
extension UIToolbar {
func hideBottomHairline() {
self.hairlineImageView?.isHidden = true
}
func showBottomHairline() {
self.hairlineImageView?.isHidden = false
}
}
extension UIView {
fileprivate var hairlineImageView: UIImageView? {
return hairlineImageView(in: self)
}
fileprivate func hairlineImageView(in view: UIView) -> UIImageView? {
if let imageView = view as? UIImageView, imageView.bounds.height <= 1.0 {
return imageView
}
for subview in view.subviews {
if let imageView = self.hairlineImageView(in: subview) { return imageView }
}
return nil
}
}
两条线的解决方案对我来说很有效。尝试在ViewDidLoad方法中添加这个:
navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
self.extendedLayoutIncludesOpaqueBars = true
这可能听起来很愚蠢,但这个发际线只在viewController视图的背景色被设置为任何颜色时出现,除了白色。得知这个事实我很震惊。
如果你想让它毫不费力地消失只要设置控制器视图背景色为WHITE color。
嗨,这适用于Swift 4。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.isTranslucent = false
}
你需要把这个放在viewDidLayoutSubviews而不是viewDidLoad
对我来说,最简单的方法是创建一个具有所需颜色的png(它只需要一个像素乘以一个像素的尺寸),然后设置backgroundImage和shadowImage:
let greenPixel = UIImage(named: "TheNameOfYourPng")
navigationBar.setBackgroundImage(greenPixel, forBarMetrics: UIBarMetrics.Default)
navigationBar.shadowImage = greenPixel