轉自: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()