ios 如何快速删除导航栏的边框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26390072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove border of the navigationBar in swift?
提问by Peter Pik
i've been trying to remove the navigationBars border without luck. I've researched and people seem to tell to set shadowImage and BackgroundImage to nil, but this does not work in my case.
我一直试图在没有运气的情况下删除导航栏边框。我已经研究过并且人们似乎告诉将 shadowImage 和 BackgroundImage 设置为 nil,但这在我的情况下不起作用。
My code
我的代码
self.navigationController?.navigationBar.barTintColor = UIColor(rgba: "#4a5866")
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: ""), forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.shadowImage = UIImage(named: "")
illustration:
插图:
回答by Nate Cook
The trouble is with these two lines:
问题在于这两行:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: ""), forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.shadowImage = UIImage(named: "")
Since you don't have an image with no name, UIImage(named: "")
returns nil
, which means the default behavior kicks in:
由于您没有没有名称的图像,因此UIImage(named: "")
返回nil
,这意味着默认行为开始:
When non-nil, a custom shadow image to show instead of the default shadow image. For a custom shadow to be shown, a custom background image must also be set with -setBackgroundImage:forBarMetrics: (if the default background image is used, the default shadow image will be used).
当非零时,显示自定义阴影图像而不是默认阴影图像。要显示自定义阴影,还必须使用 -setBackgroundImage:forBarMetrics: 设置自定义背景图像(如果使用默认背景图像,则将使用默认阴影图像)。
You need a truly empty image, so just initialize with UIImage()
:
你需要一个真正的空图像,所以只需初始化UIImage()
:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
回答by Sazzad Hissain Khan
Swift 4 & Swift 5
斯威夫特 4 和斯威夫特 5
Removing border:
去除边框:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for:.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.layoutIfNeeded()
Restoring border:
恢复边界:
self.navigationController?.navigationBar.setBackgroundImage(nil, for:.default)
self.navigationController?.navigationBar.shadowImage = nil
self.navigationController?.navigationBar.layoutIfNeeded()
回答by Steve Payne
this will remove the shadow image altogether
这将完全删除阴影图像
for parent in self.navigationController!.navigationBar.subviews {
for childView in parent.subviews {
if(childView is UIImageView) {
childView.removeFromSuperview()
}
}
}
回答by Jorge Casariego
With Swift 2 you can do it this way:
使用 Swift 2,您可以这样做:
AppDelegate file
应用委托文件
Inside func application(..., didFinishLaunchingWithOptions launchOptions:...)
在 func 应用程序中(..., didFinishLaunchingWithOptions launchOptions:...)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: .Default)
for Swift 3:
对于 Swift 3:
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
回答by Gaurav Chandarana
Just write this in the extension of UINavigationBar
在 UINavigationBar 的扩展里写这个就行了
extension UINavigationBar {
func shouldRemoveShadow(_ value: Bool) -> Void {
if value {
self.setValue(true, forKey: "hidesShadow")
} else {
self.setValue(false, forKey: "hidesShadow")
}
}
}
And in your viewController...
在你的 viewController ...
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.shouldRemoveShadow(true)
}
And to get this undone for any viewController, just pass false.
要为任何 viewController 撤消此操作,只需传递 false。
回答by gpbl
Set barStyle
to .Black
before setting the tint:
设置barStyle
来.Black
设置色调之前:
self.navigationController?.navigationBar.translucent = false
self.navigationController?.navigationBar.barStyle = .Black
self.navigationController?.navigationBar.barTintColor = UIColor.blueColor()
回答by David Baez
Luca Davanzo's answer is great, but it does not work in iOS 10. I altered it to work in iOS 10 and below.
Luca Davanzo 的回答很好,但它在 iOS 10 中不起作用。我将其更改为在 iOS 10 及更低版本中工作。
for parent in navigationController!.view.subviews {
for child in parent.subviews {
for view in child.subviews {
if view is UIImageView && view.frame.height == 0.5 {
view.alpha = 0
}
}
}
}
You can also extend UINavigationController and call this off of that. removeFromSuperview()
on the line will not work on iOS 10, so I just set the alpha to 0 so this one call is compatible everywhere.
您还可以扩展 UINavigationController 并从中调用它。removeFromSuperview()
上线将无法在 iOS 10 上运行,所以我只是将 alpha 设置为 0,这样这个调用在任何地方都兼容。
回答by Hyman Chen
Swift 5
斯威夫特 5
When using setBackgroundImage / shadowImage to hide the hairline, there's a slight delay. This method removes the delay. Credit to Chameleon Framework. This is the method they use (in ObjC)
使用 setBackgroundImage / shadowImage 隐藏发际线时,会有一点延迟。这种方法消除了延迟。感谢变色龙框架。这是他们使用的方法(在 ObjC 中)
extension UINavigationController {
func hideHairline() {
if let hairline = findHairlineImageViewUnder(navigationBar) {
hairline.isHidden = true
}
}
func restoreHairline() {
if let hairline = findHairlineImageViewUnder(navigationBar) {
hairline.isHidden = false
}
}
func findHairlineImageViewUnder(_ view: UIView) -> UIImageView? {
if view is UIImageView && view.bounds.size.height <= 1.0 {
return view as? UIImageView
}
for subview in view.subviews {
if let imageView = self.findHairlineImageViewUnder(subview) {
return imageView
}
}
return nil
}
}
回答by reza_khalafi
To remove border from UINavigationBar in Swift 3+, use:
要在 Swift 3+ 中从 UINavigationBar 中删除边框,请使用:
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
UINavigationBar.appearance().isTranslucent = false
回答by Gulz
for swift 3
快速 3
in viewDidLoad
method
在viewDidLoad
方法中
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()