我有一个应用程序,有时需要它的导航栏融入内容。
有人知道怎么去掉或者改变这个烦人的小条的颜色吗?
在下图中,我说的是根视图控制器下面1px的高度线
我有一个应用程序,有时需要它的导航栏融入内容。
有人知道怎么去掉或者改变这个烦人的小条的颜色吗?
在下图中,我说的是根视图控制器下面1px的高度线
当前回答
pxpgraphics的解决方案为Swift 2.0更新
extension UINavigationBar {
func hideBottomHairline()
{
hairlineImageViewInNavigationBar(self)?.hidden = true
}
func showBottomHairline()
{
hairlineImageViewInNavigationBar(self)?.hidden = false
}
private func hairlineImageViewInNavigationBar(view: UIView) -> UIImageView?
{
if let imageView = view as? UIImageView where imageView.bounds.height <= 1
{
return imageView
}
for subview: UIView in view.subviews
{
if let imageView = hairlineImageViewInNavigationBar(subview)
{
return imageView
}
}
return nil
}
}
extension UIToolbar
{
func hideHairline()
{
let navigationBarImageView = hairlineImageViewInToolbar(self)?.hidden = true
}
func showHairline()
{
let navigationBarImageView = hairlineImageViewInToolbar(self)?.hidden = false
}
private func hairlineImageViewInToolbar(view: UIView) -> UIImageView?
{
if let imageView = view as? UIImageView where imageView.bounds.height <= 1
{
return imageView
}
for subview: UIView in view.subviews
{
if let imageView = hairlineImageViewInToolbar(subview)
{
return imageView
}
}
return nil
}
}
其他回答
pxpgraphics对Swift 3.0的回答。
import Foundation
import UIKit
extension UINavigationBar {
func hideBottomHairline() {
let navigationBarImageView = hairlineImageViewInNavigationBar(view: self)
navigationBarImageView!.isHidden = true
}
func showBottomHairline() {
let navigationBarImageView = hairlineImageViewInNavigationBar(view: self)
navigationBarImageView!.isHidden = false
}
private func hairlineImageViewInNavigationBar(view: UIView) -> UIImageView? {
if view is 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(view: subview) {
return imageView
}
}
return nil
}
}
extension UIToolbar {
func hideHairline() {
let navigationBarImageView = hairlineImageViewInToolbar(view: self)
navigationBarImageView!.isHidden = true
}
func showHairline() {
let navigationBarImageView = hairlineImageViewInToolbar(view: self)
navigationBarImageView!.isHidden = false
}
private func hairlineImageViewInToolbar(view: UIView) -> UIImageView? {
if view is 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(view: subview) {
return imageView
}
}
return nil
}
}
对于iOS 13:
使用.shadowColor属性
如果此属性为nil或包含透明颜色,则该栏不显示阴影
例如:
let navigationBar = navigationController?.navigationBar
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.shadowColor = .clear
navigationBar?.scrollEdgeAppearance = navigationBarAppearance
对于iOS 12及以下版本:
为此,您应该设置一个自定义阴影图像。但是为了显示阴影图像,你还需要设置一个自定义背景图像,引用自苹果的文档:
要显示自定义阴影图像,必须有自定义背景图像 也可以使用setBackgroundImage(_:for:)方法进行设置。如果默认 使用背景图像,然后将使用默认阴影图像 不管这个属性的值。
So:
let navigationBar = navigationController!.navigationBar
navigationBar.setBackgroundImage(#imageLiteral(resourceName: "BarBackground"),
for: .default)
navigationBar.shadowImage = UIImage()
以上是唯一的“官方”隐藏方法。不幸的是,它消除了杆的半透明。
我不需要背景图像,只是颜色##
你有这些选择:
纯色,无半透明: navigationBar。barTintColor = uiccolor . redcolor () navigationBar。isstranslucent = false navigationBar.setBackgroundImage(UIImage(), for: .default) navigationBar。shadowImage = UIImage() 创建小背景图像充满颜色和使用它。 使用下面描述的“hack”方法。它也将保持酒吧半透明。
如何保持酒吧的半透明?# #
为了保持半透明,你需要另一种方法,它看起来像一个黑客,但工作得很好。我们试图移除的阴影是UINavigationBar下面的一个发际线UIImageView。我们可以找到它,并在需要时隐藏/显示它。
下面的说明假设你只需要在UINavigationController层次结构的一个控制器中隐藏发际线。
Declare instance variable: private var shadowImageView: UIImageView? Add method which finds this shadow (hairline) UIImageView: private func findShadowImage(under view: UIView) -> UIImageView? { if view is UIImageView && view.bounds.size.height <= 1 { return (view as! UIImageView) } for subview in view.subviews { if let imageView = findShadowImage(under: subview) { return imageView } } return nil } Add/edit viewWillAppear/viewWillDisappear methods: override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) if shadowImageView == nil { shadowImageView = findShadowImage(under: navigationController!.navigationBar) } shadowImageView?.isHidden = true } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) shadowImageView?.isHidden = false }
同样的方法也适用于UISearchBar发际线, 以及(几乎)任何你需要隐藏的东西:)
非常感谢@Leo Natan的原创想法!
我也遇到了同样的问题,但没有一个答案真正令人满意。以下是我对Swift3的看法:
func hideNavigationBarLine() {
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
}
只需在viewDidLoad()中调用它。
两条线的解决方案对我来说很有效。尝试在ViewDidLoad方法中添加这个:
navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
self.extendedLayoutIncludesOpaqueBars = true
在AppDelegate中,这已经全局地改变了导航栏的格式:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarPosition: UIBarPosition.Any, barMetrics: UIBarMetrics.Default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().barTintColor = UIColor.redColor()
UINavigationBar.appearance().translucent = false
UINavigationBar.appearance().clipsToBounds = false
UINavigationBar.appearance().backgroundColor = UIColor.redColor()
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : (UIFont(name: "FONT NAME", size: 18))!, NSForegroundColorAttributeName: UIColor.whiteColor()] }
还没有在特定的VC上实现任何不同的东西,但这将帮助90%的人