我已经找到了关于这个问题的好答案,请看How to hide iOS7 UINavigationBar 1px bottom line
但我想知道如何用swift实现它,我已经尝试过这种方式
func findHairlineImageViewUnder(view:UIView!) {
if view is UIImageView && view.bounds.size.height <= 1.0 {
return view
}
var subview: UIView
for subview in view.subviews {
var imageView:UIImageView = self.findHairlineImageViewUnder(subview)
if imageView {
return imageView
}
}
}
我无法成功,因为编译器告诉我
我知道为什么会出现这些错误,但我该如何解决?
答案 0 :(得分:2)
此扩展应该这样做。
extension UINavigationController {
func hairLine(hide hide: Bool) {
//hides hairline at the bottom of the navigationbar
for subview in self.navigationBar.subviews {
if subview.isKindOfClass(UIImageView) {
for hairline in subview.subviews {
if hairline.isKindOfClass(UIImageView) && hairline.bounds.height <= 1.0 {
hairline.hidden = hide
}
}
}
}
}
}
就这样称呼它:
navigationController?.hairLine(hide: true)
答案 1 :(得分:0)
您的代码中存在一些问题。
UIView?
;它应该是可选的,因为您可能无法找到该视图,并且需要返回nil
。subview
循环之前声明for
; for
循环将隐式执行。imageView
声明为UIImageView
将无效。但是,你首先不需要把它变为UIImageView
,你可以迅速处理它。for
循环内部,并执行if let foundView = self.findHairlineImageViewUnder(subview) { ... }
return nil
。以下是一个包含上述修复的工作实现:
func findHairlineImageViewUnder(view:UIView!) -> UIView? {
if view is UIImageView && view.bounds.size.height <= 1.0 {
return view
}
for subview in view.subviews as [UIView] {
if let foundView = self.findHairlineImageViewUnder(subview) {
return foundView
}
}
return nil
}