如何使UINavigationBar透明?虽然我希望它的栏项仍然可见。
当前回答
这适用于Swift 5。
// Clear the background image.
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
// Clear the shadow image.
navigationController?.navigationBar.shadowImage = UIImage()
// Ensure the navigation bar is translucent.
navigationController?.navigationBar.isTranslucent = true
其他回答
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)
}
}
}
}
下面的代码扩展了为这个线程选择的顶部答案,以消除底部边框并设置文本颜色:
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()
如果有人想知道如何在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的系统不透明度。
试试下面这段代码:
self.navigationController.navigationBar.translucent = YES;
我知道这个话题是旧的,但如果人们想知道如何在不重载drawRect方法的情况下完成。
这是你需要的:
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.opaque = YES;
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
推荐文章
- 更改UITextField和UITextView光标/插入符颜色
- 'Project Name'是通过优化编译的——步进可能会表现得很奇怪;变量可能不可用
- 如何设置回退按钮文本在Swift
- 模拟器慢动作动画现在打开了吗?
- 如何为TableView创建NSIndexPath
- 滑动删除和“更多”按钮(就像iOS 7的邮件应用程序)
- 如何比较两个nsdate:哪个是最近的?
- 使UINavigationBar透明
- 如何改变推和弹出动画在一个基于导航的应用程序
- 删除/重置核心数据中的所有条目?
- Swift to Objective-C头未在Xcode 6中创建
- setNeedsLayout vs. setNeedsUpdateConstraints和layoutIfNeeded vs. updateConstraintsIfNeeded
- 不区分大小写的比较
- 我怎么能得到一个uiimage的高度和宽度?
- 我如何模仿地图应用程序的底部表格?