iOS 15 导航栏透明

人气:1,013 发布:2022-10-16 标签: swift ios15 uinavigationcontroller xcode13

问题描述

我的 iOS 应用为 UI 使用故事板,并为导航栏的背景颜色使用自定义色调.

My iOS app uses the storyboard for the UI and uses a custom tint for the background color of the navigation bar.

我在 Xcode 13 beta 5 上测试了我的应用,导航栏是白色"的.并且导航栏上的文字不可见.

I have tested my app on the Xcode 13 beta 5 and the navigation bar is "white" and the text on the navigation bar is not visible.

在苹果开发者论坛 https://developer.apple.com/forums/thread/682420 它声明 在 iOS 15 中,UIKit 已将默认情况下会产生透明背景的 scrollEdgeAppearance 的使用扩展到所有导航栏."要恢复旧外观,您必须采用新的 UINavigationBar 外观 API

In the apple developer forum at https://developer.apple.com/forums/thread/682420 it states that "In iOS 15, UIKit has extended the usage of the scrollEdgeAppearance, which by default produces a transparent background, to all navigation bars." To restore the old look, you must adopt the new UINavigationBar appearance APIs

我将以下代码(来自上面的链接)添加到 App Delegate application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions":

I added the following code (from the link above) to the App Delegate "application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions":

        if #available(iOS 13, *) {
            let navigationController = UINavigationController(navigationBarClass: nil, toolbarClass: nil)
            let navigationBar = navigationController.navigationBar
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = UIColor(red: 0.0/255.0, green: 125/255.0, blue: 0.0/255.0, alpha: 1.0)
            navigationBar.standardAppearance = appearance;
            navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
            navigationBar.isTranslucent = false
        }

这并不能解决问题.我仍然在故事板编辑器中为导航栏设置了自定义色调.我需要删除自定义色调还是我实现的外观 API 错误?

This does not fix the problem. I still have the custom tint set in the storyboard editor for the navigation bar. Do I need to remove the custom tint or am I implementing the appearance API wrong?

推荐答案

Swift

// White non-transucent navigatio bar, supports dark appearance
if #available(iOS 15, *) {
    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    UINavigationBar.appearance().standardAppearance = appearance
    UINavigationBar.appearance().scrollEdgeAppearance = appearance
}

目标-c

if (@available(iOS 15.0, *)) {
    UINavigationBarAppearance *navBarAppearance = [[UINavigationBarAppearance alloc] init];
  navBarAppearance.backgroundColor = [UIColor redColor];
    [navBarAppearance configureWithOpaqueBackground];
    [UINavigationBar appearance].standardAppearance = navBarAppearance;
    [UINavigationBar appearance].scrollEdgeAppearance = navBarAppearance;
}

712