如何使UINavigationBar透明?虽然我希望它的栏项仍然可见。
当前回答
解决方案- Swift 5 - iOS 13+
根据文档,在你的UIViewController子类中:
override func viewDidLoad()
{
super.viewDidLoad()
let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()
//appearance.backgroundColor = UIColor.clear
navigationItem.compactAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
navigationItem.standardAppearance = appearance
//...
}
只是为了澄清,这使得UINavigationBar完全透明。栏按钮项仍然可见,并正常工作。
什么不起作用
override func viewDidLoad()
{
super.viewDidLoad()
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.isOpaque = false
//...
}
这让我意识到我其实不知道透明RIP和半透明RIP之间的区别。
参考文献
https://developer.apple.com/documentation/uikit/uinavigationcontroller/customizing_your_app_s_navigation_bar
https://www.lexico.com/en/definition/transparent
https://www.lexico.com/en/definition/translucent
更新08/10/2021
在以我提供的方式设置外观后,更改navigationItem栏按钮将重置外观,您必须再次执行此操作。
其他回答
你的意思是完全透明,还是使用照片应用程序中看到的半透明黑色?后者你可以通过设置它的barStyle属性为uibarstyleblack半透明来实现。前……我不太确定。如果您希望其中的项仍然可见,您可能必须在栏的视图层次结构中进行一些挖掘,并删除包含其背景的视图。
如果有人想知道如何在iOS 7+中实现这一点,这里有一个解决方案(iOS 6也兼容)
在objective - c中
[self.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;
swift 3 (iOS 10)
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true
在swift 2中
self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.translucent = true
讨论
将导航栏上的半透明设置为YES就可以了,这是由于UINavigationBar文档中讨论的行为。我将在这里报告相关片段:
如果在具有不透明自定义背景图像的导航栏上将此属性设置为YES,则导航栏将对图像应用小于1.0的系统不透明度。
extension UINavigationBar {
var isTransperent: Bool {
get {
return false // Just to satisfy property
}
set {
if newValue {
self.shadowImage = UIImage()
self.isTranslucent = true
self.setBackgroundImage(UIImage(), for: .default)
} else {
self.shadowImage = UIImage()
self.isTranslucent = false
self.setBackgroundImage(nil, for: .default)
}
}
}
}
解决方案- Swift 5 - iOS 13+
根据文档,在你的UIViewController子类中:
override func viewDidLoad()
{
super.viewDidLoad()
let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()
//appearance.backgroundColor = UIColor.clear
navigationItem.compactAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
navigationItem.standardAppearance = appearance
//...
}
只是为了澄清,这使得UINavigationBar完全透明。栏按钮项仍然可见,并正常工作。
什么不起作用
override func viewDidLoad()
{
super.viewDidLoad()
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.isOpaque = false
//...
}
这让我意识到我其实不知道透明RIP和半透明RIP之间的区别。
参考文献
https://developer.apple.com/documentation/uikit/uinavigationcontroller/customizing_your_app_s_navigation_bar
https://www.lexico.com/en/definition/transparent
https://www.lexico.com/en/definition/translucent
更新08/10/2021
在以我提供的方式设置外观后,更改navigationItem栏按钮将重置外观,您必须再次执行此操作。
下面的代码扩展了为这个线程选择的顶部答案,以消除底部边框并设置文本颜色:
The last two coded lines of this code set transparency. I borrowed that code from this thread and it worked perfectly! The "clipsToBounds" property was code I found which got rid of the bottom border line with OR without transparency set (so if you decide to go with a solid white/black/etc. background instead, there will still be no border line). The "tintColor" line (2nd coded line) set my back button to a light grey I kept barTintColor as a backup. I don't know why transparency would not work, but if it doesn't, I want my bg white as I used to have it let navigationBarAppearace = UINavigationBar.appearance() navigationBarAppearace.tintColor = UIColor.lightGray navigationBarAppearace.barTintColor = UIColor.white navigationBarAppearace.clipsToBounds = true navigationBarAppearace.isTranslucent = true navigationBarAppearace.setBackgroundImage(UIImage(), for: .default) navigationBarAppearace.shadowImage = UIImage()
推荐文章
- 保留HTML字体大小时,iPhone的方向从纵向改变为横向
- iPhone上UIView和UILabels的渐变
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?
- 如何在iPhone/iOS上删除电话号码的蓝色样式?
- 检测视网膜显示
- 如何在UIImageView中动画图像的变化?