如何隐藏UINavigationbar下面的黑线,swift版

 我来答
星月小木木
2016-01-29 · TA获得超过3.2万个赞
知道大有可为答主
回答量:2.4万
采纳率:0%
帮助的人:1亿
展开全部
先建一个UIBarExtension.swift的文件来扩展UINavigationBar
import Foundation
import UIKit

extension 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?.navigationBar
navigationBar?.hideBottomHairline()
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式