UINavigationBar颜色和透明之间的过渡 - bug [iOS 11]

时间:2018-03-20 07:49:03

标签: ios swift uikit ios11

当我按UIViewController并将UINavigationBar颜色更改为透明时,我在iOS 11上发现了一种奇怪的行为。重要的是我使用largeTitles

  1. 我想将导航栏的红色更改为透明,这样可以正常工作。

  2. 但是,如果我点击backButton,再次禁用透明样式和红色样式。 ViewController上的NavigationBar不是红色但仍然是透明的。

  3. 正如@Menoor Ranpura建议我添加另一行,在backgroundColor中设置一个UIViewController视图 - 当你在{{1}上设置相同颜色时这是很好的解决方法}。但是,它不是问题的解决方案,因为导航栏的大部分仍然是透明的。为背景设置不同的颜色时可以看到它。例如,我设置了黄色。你可以在这里看到这个例子:

  4. enter image description here

    问题

    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的屏幕:

    enter image description here

    有趣的事实是,有一种叫做backgroundColor的东西是透明的。 如何访问它?

    相关问题:

    您可以在此处找到示例项目:https://github.com/kamwysoc/LargeTitlesNavigationBarTransition

    更新参考@Menoor Ranpura回答

    @Menoor Ranpura建议的代码是一种解决方法。它不是像_UINavigationBarLargeTitleView那样在backgroundColor视图上设置相同UIViewController的解决方案。但是,我更进一步,我改变了与UINavigationBar控制器不同的颜色。正如您在上面的gif上看到的那样,当UINavigationBar出现时,导航栏变为黄色 - 这意味着它是透明的 - 因为我们能够看到黄色的视图背景。

2 个答案:

答案 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;
    }
}];