【问题标题】:iOS13 Navigation bar large titles not covering status bariOS13导航栏大标题不覆盖状态栏
【发布时间】:2019-11-16 13:04:33
【问题描述】:

在ios13,iphone x上,大标题导航不会覆盖状态栏,但是当滚动和过渡到传统导航栏时,它可以完美运行。这不会影响没有缺口的设备。

大标题

传统导航栏

它都嵌入在导航控制器中,所以我不知道为什么会发生这种情况。干杯

【问题讨论】:

  • 导航栏的行为在 IOS 13 中发生了变化,所以这可能是故意的
  • @matt 我认为不是 - 做出如此丑陋的改变似乎很不苹果 + 正常的导航栏采取正常的行为;为什么大标题会不同?
  • 您之前是如何覆盖导航栏背景颜色的?
  • @Sebastian 它只是我发现的标准,只有更新到 xcode 11 和 ios 13 才会出现这个问题。

标签: swift


【解决方案1】:

在 iOS 13 之前自定义 UINavigationBar 的官方方法是这样的:

// text/button color
UINavigationBar.appearance().tintColor = .white
// background color
UINavigationBar.appearance().barTintColor = .purple
// required to disable blur effect & allow barTintColor to work
UINavigationBar.appearance().isTranslucent = false

iOS 13 改变了导航栏的工作方式,因此您需要做些不同的事情来支持新旧版本:

if #available(iOS 13.0, *) {
    let appearance = UINavigationBarAppearance()
    appearance.backgroundColor = .purple
    appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]

    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().standardAppearance = appearance
    UINavigationBar.appearance().compactAppearance = appearance
    UINavigationBar.appearance().scrollEdgeAppearance = appearance
} else {
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().barTintColor = .purple
    UINavigationBar.appearance().isTranslucent = false
}

【讨论】:

  • 更改为正确答案,因为它稍微更传统并且更可靠。谢谢
  • 你有没有想过如何改变后退按钮阵列的颜色?我可以通过设置foregroundColor 来更改标题的颜色,但这不会改变箭头的颜色(或任何其他带有图标的按钮)。
  • 这可行,但现在我无法获得半透明导航栏
  • @MobileMon 您当前的彩色但半透明导航栏代码是什么?
  • @user3626411 如果你想模仿苹果的半透明导航栏,只需删除这行 "UINavigationBar.appearance().barTintColor = MyColor ": mobilemon.code.blog/2019/10/11/…
【解决方案2】:

使用我的扩展 iOS 13 Swift 5 测试

extension UIViewController {
func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
    if #available(iOS 13.0, *) {
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithOpaqueBackground()
        navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
        navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
        navBarAppearance.backgroundColor = backgoundColor

        navigationController?.navigationBar.standardAppearance = navBarAppearance
        navigationController?.navigationBar.compactAppearance = navBarAppearance
        navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance

        navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
        navigationController?.navigationBar.isTranslucent = false
        navigationController?.navigationBar.tintColor = tintColor
        navigationItem.title = title

    } else {
        // Fallback on earlier versions
        navigationController?.navigationBar.barTintColor = backgoundColor
        navigationController?.navigationBar.tintColor = tintColor
        navigationController?.navigationBar.isTranslucent = false
        navigationItem.title = title
    }
}}

使用方法:

configureNavigationBar(largeTitleColor: .yourColor, backgoundColor: .yourColor, tintColor: .yourColor, title: "yuorTitle", preferredLargeTitle: true)

如果你想要轻量的内容,在 info.plist 中将基于 ViewController 的状态栏设置为 NO

如果您不希望 largeTitles 将其设置为 false

用于半透明更改 navBarAppearance.configureWithOpaqueBackground() 中:

navBarAppearance.configureWithDefaultBackground()
navigationController?.navigationBar.isTranslucent = true

在调用中设置背景颜色为.clear

更新: 如果您想在第一个控制器中使用导航控制器和大标题,请不要忘记在 Scene Delegate 中设置启动控制器,如下所示:

guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
let vC = UINavigationController(rootViewController: YourFirstViewController())
window?.rootViewController = vC

希望得到帮助:)

【讨论】:

  • 你很棒?
【解决方案3】:

如需完整的应用程序导航栏支持,请在您的代码中添加此扩展。

import UIKit
extension UIViewController {


    open func showNavigationBar(_ large: Bool,
                                _ animated: Bool,
                                titleColor: UIColor,
                                barTintColor: UIColor,
                                fontSize: CGFloat) {

        navigationController?.navigationBar.barTintColor = barTintColor
        navigationController?.navigationBar.backgroundColor = barTintColor
        navigationController?.navigationBar.isTranslucent = true
        self.navigationController?.setNavigationBarHidden(false, animated: animated)
        if large {
            self.navigationController?.navigationBar.prefersLargeTitles = true
            if #available(iOS 13.0, *) {
                let appearance = UINavigationBarAppearance()
                appearance.backgroundColor = barTintColor
                appearance.titleTextAttributes = [.foregroundColor: titleColor]
                appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor,
                                                       NSAttributedString.Key.font: UIFont(resource: R.font.robotoMedium, size: fontSize)!]

                navigationController?.navigationBar.standardAppearance = appearance
                navigationController?.navigationBar.compactAppearance = appearance
                navigationController?.navigationBar.scrollEdgeAppearance = appearance
            } else {
                self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor,
                                                                                     NSAttributedString.Key.font: UIFont(resource: R.font.robotoMedium, size: fontSize)!]
            }
        } else {
            self.navigationController?.navigationBar.prefersLargeTitles = false
            self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor,
                                                                            NSAttributedString.Key.font: UIFont(resource: R.font.robotoMedium, size: 20.0)!]
        }
    }
}

然后简单的调用这个方法

self.showNavigationBar(true, true, titleColor: UIColor.blue, barTintColor: UIColor.red, fontSize: 32.0)

【讨论】:

    猜你喜欢
    • 2021-05-08
    • 2018-08-02
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多