【发布时间】:2013-10-14 03:38:49
【问题描述】:
我有一个应用有时需要其导航栏才能与内容融为一体。
有谁知道如何摆脱这个烦人的小酒吧或改变它的颜色?
在下图的情况下,我说的是“根视图控制器”下方的 1px 高度线
【问题讨论】:
-
导航高度如何增加1px?
标签: ios objective-c swift ipad uinavigationbar
我有一个应用有时需要其导航栏才能与内容融为一体。
有谁知道如何摆脱这个烦人的小酒吧或改变它的颜色?
在下图的情况下,我说的是“根视图控制器”下方的 1px 高度线
【问题讨论】:
标签: ios objective-c swift ipad uinavigationbar
你应该在 UISearchBar 的底部添加一个视图
let rect = searchController.searchBar.frame;
let lineView : UIView = UIView.init(frame: CGRect.init(x: 0, y: rect.size.height-1, width: rect.size.width, height: 1))
lineView.backgroundColor = UIColor.init(hexString: "8CC73E")
searchController.searchBar.addSubview(lineView)
【讨论】:
在 Swift 3.0 中
通过将以下代码添加到您的应用程序函数来编辑您的AppDelegate.swift:
// Override point for customization after application launch.
// Remove border in navigationBar
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
【讨论】:
在 Swift 3 中我们这样做
对于任何视图控制器:
navigationBar.shadowImage = UIImage()
setBackgroundImage(UIImage(), for: .default)
对于整个应用:
UINavigationBar.appearance().setBackgroundImage(UIImage(),barMetrics: .Default)
UINavigationBar.appearance().shadowImage = UIImage()
【讨论】:
我遇到了同样的问题,没有一个答案是真正令人满意的。这是我对 Swift3 的看法:
func hideNavigationBarLine() {
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
}
只需在 viewDidLoad() 中调用它。
【讨论】:
在 Xamarin 表单 中,这对我有用。只需将其添加到 AppDelegate.cs:
UINavigationBar.Appearance.ShadowImage = new UIImage();
【讨论】:
斯威夫特 4 //用于隐藏导航栏阴影线
navigationController?.navigationBar.shadowImage = UIImage()
【讨论】:
您好,这适用于 Swift 4。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.isTranslucent = false
}
你需要把它放在 viewDidLayoutSubviews 而不是 viewDidLoad
【讨论】:
这是一种不使用任何图像的方法,这是唯一对我有用的方法:
self.navigationController.navigationBar.layer.shadowOpacity = 0;
不幸的是,您需要在希望该行不出现的每个文件上执行此操作。在appDelegate 中没有办法这样做。
编辑:
不需要将shadowColor 设置为nil,这是您唯一需要的行。
【讨论】:
Swift 4 测试 单线解决方案
在Viewdidload()
为键“hidesShadow”设置导航控制器的用户默认值 true
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
}
【讨论】:
Swift 4.2 中的解决方案:
private func removeHairlineFromNavbar() {
UINavigationBar.appearance().setBackgroundImage(
UIImage(),
for: .any,
barMetrics: .default)
UINavigationBar.appearance().shadowImage = UIImage()
}
只要把这个函数放在第一个Viewcontroller中,在viewdidload中调用
【讨论】:
从 iOS 13 开始,有一个系统 API 可以设置或移除阴影
UIKit 使用 shadowImage 和 shadowColor 属性来确定阴影的 外貌。当 shadowImage 为 nil 时,条形图显示默认的阴影着色 根据 shadowColor 属性中的值。如果 shadowColor 为 nil 或 包含 clearColor 颜色,条形图不显示阴影。
let appearance = UINavigationBarAppearance()
appearance.shadowImage = nil
appearance.shadowColor = nil
navigationController.navigationBar.standardAppearance = appearance
https://developer.apple.com/documentation/uikit/uibarappearance/3198009-shadowimage
【讨论】:
UINavigationBarAppearance 标题中!!我永远找不到它。谢谢,这解决了我的问题 100%
创建一个扩展:
extension UIImage {
class func hideNavBarLine(color: UIColor) -> UIImage? {
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
context?.fill(rect)
let navBarLine = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return navBarLine
}
}
将此添加到viewDidLoad():
self.navigationController?.navigationBar.shadowImage = UIImage.hideNavBarLine(color: UIColor.clear)
【讨论】:
适合我的两行解决方案。尝试在 ViewDidLoad 方法中添加这个:
navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
self.extendedLayoutIncludesOpaqueBars = true
【讨论】:
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = Colors.color_app
appearance.titleTextAttributes = [.foregroundColor : UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor : UIColor.white]
appearance.shadowColor = .clear
appearance.shadowImage = UIImage()
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
} else {
UINavigationBar.appearance().barTintColor = Colors.color_app
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
if #available(iOS 11.0, *) {
UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
}
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
}
【讨论】:
编写你自己的初始化器:D
import Foundation
import UIKit
extension UINavigationController {
convenience init(rootViewController : UIViewController, hidesShadow : Bool) {
self.init(rootViewController : rootViewController)
self.navigationBar.setValue(hidesShadow, forKey: "hidesShadow")
if hidesShadow {
self.extendedLayoutIncludesOpaqueBars = true
self.navigationBar.isTranslucent = false
}
}
}
【讨论】:
不要使用navigationController?.navigationBar.setValue(true, forKey: "hidesShadow") 非常重要,因为在任何时候,Apple 都可以删除“hidesShadow”键路径。如果他们这样做,任何使用此调用的应用程序都会中断。由于您没有访问类的直接 API,因此此调用可能会遭到 App Store 拒绝。
从 iOS 13 开始,为确保效率,您可以执行以下操作:
navigationBar.standardAppearance.shadowColor = nil
【讨论】:
isTranslucent = false 的设置,背景颜色将被更改。到目前为止,只有“不使用”的答案对我有用,即使在最新的 iOS 14.4 中也是如此
这里有一个非常重要的注意事项 - 与直接更改 navigationBar 相比,更改 UIViewController 的 navigationItem 的外观要灵活得多。
你为什么这么问?
原因很简单,navigationItem 与单个 UIViewController 相关联,并代表特定 UIViewController 的 navigationBar 的状态。这很大,因为您不必处理 viewWillAppear(或类似的东西)内不同视图控制器之间的导航栏更改,就像您对 navigationBar 进行变异时那样;请记住,它在给定导航堆栈 (UINavigationController) 的所有视图控制器之间共享,并且在一个地方更改它会更改堆栈之前的所有视图控制器。
您只需为特定的视图控制器设置正确的UINavigationBarAppearance,UIKit 就会正确更新导航栏样式,具体取决于哪个视图控制器当前是导航堆栈上的顶部视图控制器。
navigationItem.standardAppearance` = `UINavigationBarAppearance()
【讨论】:
对于 iOS 13+,诀窍是使用 TransparentBackground 初始化 'UINavigationBarAppearance'。然后就可以轻松去掉导航栏的水平线了。
let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()
appearance.backgroundColor = .green // Required background color
最后,按照苹果的建议为导航项添加外观变化。
self.navigationItem.standardAppearance = appearance
self.navigationItem.scrollEdgeAppearance = appearance
self.navigationItem.compactAppearance = appearance
【讨论】: