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

Har*_*y J 29 swift

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

大标题 在此处输入图片说明

传统导航栏

它们全都嵌入在导航控制器中,所以我不知道为什么会这样。干杯

Seb*_*ian 45

在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
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)


Fab*_*bio 15

使用我的扩展 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
    }
}}
Run Code Online (Sandbox Code Playgroud)

如何使用:

configureNavigationBar(largeTitleColor: .yourColor, backgoundColor: .yourColor, tintColor: .yourColor, title: "yuorTitle", preferredLargeTitle: true)
Run Code Online (Sandbox Code Playgroud)

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

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

对于半透明更改 navBarAppearance.configureWithOpaqueBackground() 在:

navBarAppearance.configureWithDefaultBackground()
navigationController?.navigationBar.isTranslucent = true
Run Code Online (Sandbox Code Playgroud)

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

更新:如果你想在第一个控制器上使用导航控制器和大标题,不要忘记在场景委托中设置启动控制器,如下所示:

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

希望这有帮助:)

  • 你很棒 (2认同)