iOS13导航栏大标题未覆盖状态栏

iOS13 Navigation bar large titles not covering status bar

提问人:Harry J 提问时间:7/6/2019 最后编辑:McDonal_11Harry J 更新时间:3/29/2020 访问量:21721

问:

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

大型标题enter image description here

传统导航栏

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

迅速

评论

0赞 matt 7/6/2019
导航栏行为在 IOS 13 中发生了变化,所以也许这是故意的
0赞 Harry J 7/6/2019
@matt 我认为不是 - 做出如此丑陋的更改似乎太不合时宜了 + 正常的导航栏采用正常行为;为什么大标题会有所不同?
0赞 Sebastian 7/14/2019
您以前是如何覆盖导航栏背景颜色的?
0赞 Harry J 7/14/2019
@Sebastian它只是我发现的标准,但只有更新到 xcode 11 和 ios 13 才能解决这个问题。

答:

102赞 Sebastian 7/15/2019 #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
}

评论

1赞 Harry J 7/15/2019
更改为正确答案,因为它稍微更传统并且更依赖。谢谢
0赞 Koen. 9/2/2019
你有没有想过如何改变后退按钮数组的颜色?我可以通过设置 来更改标题的颜色,但这不会更改箭头(或任何其他带有图标的按钮)的颜色。foregroundColor
0赞 MobileMon 9/4/2019
这有效,但现在我无法获得半透明的导航栏
1赞 Sebastian 9/8/2019
@MobileMon 您当前彩色但半透明的导航栏的代码是什么?
1赞 MobileMon 1/2/2020
@user3626411如果你想模仿苹果的半透明导航栏,只需删除这一行 “UINavigationBar.appearance().barTintColor = MyColor ”: mobilemon.code.blog/2019/10/11/...
15赞 Fabio 10/13/2019 #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)

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

如果您不想要 largeTitles,请将其设置为 false

对于 tranlsucent,将 navBarAppearance.configureWithOpaqueBackground() 更改为:

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

在通话中,将背景颜色设置为 .clear

更新: 如果你想从导航控制器和大型字幕开始,不要忘记在场景委托中设置启动控制器,如下所示:

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

希望这能:)

评论

2赞 Fahim Rahman 1/7/2021
你很棒 👍
1赞 Kiran Sarvaiya 11/4/2019 #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)