Swift 3删除导航栏下面的行

问题描述 投票:1回答:4

当我的项目在Swift 2中时,我有这个代码有效:

extension UINavigationController {
func hairLine(hide: Bool) {
    //hides hairline at the bottom of the navigationbar

    for subview in self.navigationBar.subviews {
        if subview.isKind(of: UIImageView.self) {
            for hairline in subview.subviews {
                if hairline.isKind(of: UIImageView.self) && hairline.bounds.height <= 1.0 {
                    hairline.isHidden = hide
                }
            }
        }
    }
}

}

但是现在有些东西发生了变化而且不起作用。不确定是因为Swift 3,还是iOS10,或者我现在正在测试7plus vs 6s,但它不再有效。我会在其中调用正在显示的视图控制器的viewWillAppear。我在这里看到一个答案,说要使用

    UINavigationBar.appearance().shadowImage = UIImage()
    UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)

但这没效果。我尝试用这两行替换旧hairLine()的内容,尝试将它们直接放在viewWillAppear和viewDidAppear中,但仍然不适合我。

swift swift3 uinavigationbar
4个回答
5
投票

试试这个

self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")

2
投票

Before:

enter image description here

After:

enter image description here

Code:

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "Hello World"

    let navbarColor = UIColor(colorLiteralRed: (247/255), green: (247/255), blue: (247/255), alpha: 1)
    let image = UIImage()

    navigationController?.navigationBar.setBackgroundImage(image, for: .default)
    navigationController?.navigationBar.shadowImage = image
    navigationController?.navigationBar.backgroundColor = navbarColor

    let statusBarHeight = UIApplication.shared.statusBarFrame.height
    let statusBarWidth = UIScreen.main.bounds.size.width

    let statusBarView = UIView(frame: CGRect(x: 0, y: 0, width: statusBarWidth, height: statusBarHeight))
    statusBarView.backgroundColor = navbarColor

    view.addSubview(statusBarView)
  }

0
投票

尝试:

self.navigationController?.navigationBar.setBackgroundImage(_:UIImage(),
        for: .any,
        barMetrics: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()

viewDidLoad()


-1
投票

试试这个

UINavigationBar.appearance().setBackgroundImage(_:
    nil,
    for: .any,
    barMetrics: .default)

UINavigationBar.appearance().shadowImage = nil
© www.soinside.com 2019 - 2024. All rights reserved.