当我按UIViewController
并将UINavigationBar
颜色更改为透明时,我在iOS 11上发现了一种奇怪的行为。重要的是我使用largeTitles
。
我想将导航栏的红色更改为透明,这样可以正常工作。
但是,如果我点击backButton,再次禁用透明样式和红色样式。 ViewController上的NavigationBar不是红色但仍然是透明的。
正如@Menoor Ranpura建议我添加另一行,在backgroundColor
中设置一个UIViewController
视图 - 当你在{{1}上设置相同颜色时这是很好的解决方法}。但是,它不是问题的解决方案,因为导航栏的大部分仍然是透明的。为背景设置不同的颜色时可以看到它。例如,我设置了黄色。你可以在这里看到这个例子:
当UINavigationBar
设置为prefersLargeTitles
时,如何正确地将导航栏颜色从透明更改为红色?
true
class ExampleTableTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseID")
navigationController?.navigationBar.redNavigationBar()
title = "Red NavBar"
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
view.backgroundColor = .yellow
navigationController?.navigationBar.redNavigationBar()
}
}
//Transparent Navigation bar controller
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "Transparent NavBar"
view.backgroundColor = .blue
self.navigationController?.navigationBar.prefersLargeTitles = true
}
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.navigationBar.transparentNavigationBar()
}
}
extension UINavigationBar {
func redNavigationBar() {
setBackgroundImage(nil, for: .default)
shadowImage = nil
prefersLargeTitles = true
tintColor = .white
largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
barTintColor = .red
}
func transparentNavigationBar() {
setBackgroundImage(UIImage(), for: .default)
shadowImage = UIImage()
}
}
设置为prefersLargeTitles
false
设置为prefersLargeTitles
时,一切正常,但true
更改介于非透明颜色之间。例如,如果在绿色< - >之间切换。黄navigationBar
。它不是解决方案,而是一种解决方法。 在这里,您可以看到XCode的屏幕:
有趣的事实是,有一种叫做backgroundColor
的东西是透明的。 如何访问它?
您可以在此处找到示例项目:https://github.com/kamwysoc/LargeTitlesNavigationBarTransition
@Menoor Ranpura建议的代码是一种解决方法。它不是像_UINavigationBarLargeTitleView
那样在backgroundColor
视图上设置相同UIViewController
的解决方案。但是,我更进一步,我改变了与UINavigationBar
控制器不同的颜色。正如您在上面的gif上看到的那样,当UINavigationBar
出现时,导航栏变为黄色 - 这意味着它是透明的 - 因为我们能够看到黄色的视图背景。
答案 0 :(得分:1)
在 viewWillAppear
中调用 greenNavigationBar()override func viewWillAppear(_ animated: Bool)
{
view.backgroundColor = .red // this is kind of a workaround, However if you set color which will be different that `UINavigationBar` color you see that `largeTitle` is transparent because it shows a `backgroundColor` of a ViewController view.
navigationController?.navigationBar.greenNavigationBar()
title = "Green Title"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(showTransparentViewController))
}
答案 1 :(得分:0)
这是您访问_UINavigationBarLargeTitleView
的方式。我认为您应该提交雷达,如果您还没有这样做的话,因为设置此私有ivar的背景颜色只是一个小问题,而整个问题仍然存在。
迅速3/4:
if let navigationBar = navigationController?.navigationBar,
let titleView = navigationBar.subviews.first(where: {type(of: $0) == NSClassFromString("_UINavigationBarLargeTitleView")}) {
}
Objective-C:
__block UIView *titleView;
[navigationController.navigationBar.subviews enumerateObjectsUsingBlock:^(UIView *subview, NSUInteger index, BOOL *stop) {
Class _UINavigationBarLargeTitleView = NSClassFromString(@"_UINavigationBarLargeTitleView");
if ([subview isKindOfClass:_UINavigationBarLargeTitleView]) {
titleView = subview;
*stop = YES;
}
}];