如何隱藏UINavigationbar下面的黑線,swift版

來源:互聯網
上載者:User


轉自:http://www.starming.com/index.php?v=index&view=53


先建一個UIBarExtension.swift的檔案來擴充UINavigationBar

import Foundationimport UIKitextension UINavigationBar {    func hideBottomHairline() {        let navigationBarImageView = hairlineImageViewInNavigationBar(self)        navigationBarImageView!.hidden = true    }    func showBottomHairline() {        let navigationBarImageView = hairlineImageViewInNavigationBar(self)        navigationBarImageView!.hidden = false    }    private func hairlineImageViewInNavigationBar(view: UIView) -> UIImageView? {        if view.isKindOfClass(UIImageView) && view.bounds.height <= 1.0 {            return (view as UIImageView)        }        let subviews = (view.subviews as [UIView])        for subview: UIView in subviews {            if let imageView: UIImageView = hairlineImageViewInNavigationBar(subview)? {                return imageView            }        }        return nil    }}extension UIToolbar {    func hideHairline() {        let navigationBarImageView = hairlineImageViewInToolbar(self)        navigationBarImageView!.hidden = true    }    func showHairline() {        let navigationBarImageView = hairlineImageViewInToolbar(self)        navigationBarImageView!.hidden = false    }    private func hairlineImageViewInToolbar(view: UIView) -> UIImageView? {        if view.isKindOfClass(UIImageView) && view.bounds.height <= 1.0 {            return (view as UIImageView)        }        let subviews = (view.subviews as [UIView])        for subview: UIView in subviews {            if let imageView: UIImageView = hairlineImageViewInToolbar(subview)? {                return imageView            }        }        return nil    }}

再在viewDidLoad裡使用

let navigationBar = self.navigationController?.navigationBarnavigationBar?.hideBottomHairline()

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.