【问题标题】:UINavigationBar transition between color and transparent - bug [iOS 11]UINavigationBar 颜色和透明之间的过渡 - 错误 [iOS 11]
【发布时间】:2018-08-28 22:48:32
【问题描述】:

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

  1. 我想将导航栏的红色更改为透明,效果很好。

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

  3. 正如@Menoor Ranpura 建议的那样,我添加了另一行,它还设置了UIViewController 中的backgroundColor 视图-当您设置与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 更改在非透明颜色之间。例如,如果在绿色 黄色
  • 之间切换
  • 我不想将backgroundColor 设置为可见。这不是一个解决方案,而是一种解决方法。

在这里你可以看到来自 XCode 的屏幕:

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

相关问题:

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


更新指的是@Menoor Ranpura 的回答

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

【问题讨论】:

  • iOS 12 中仍然出现Bug。你提交雷达了吗?

标签: ios swift uikit ios11


【解决方案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))
}

【讨论】:

  • 你在我的示例项目中测试过吗?因为它不起作用。
  • 你能分享整个代码吗?或者也许你可以用你的修复做一个拉请求?我会查一下! :)
【解决方案2】:

以下是访问_UINavigationBarLargeTitleView 的方法。如果你还没有提交雷达,我认为你应该提交一个雷达,因为设置这个私有 ivar 的背景颜色只是一个 hack,而且整个问题仍然存在。

斯威夫特 3/4:

if let navigationBar = navigationController?.navigationBar,
    let titleView = navigationBar.subviews.first(where: {type(of: $0) == NSClassFromString("_UINavigationBarLargeTitleView")}) {

}

目标-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;
    }
}];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    相关资源
    最近更新 更多