【问题标题】:Transparent TabBar when popping Child NavigationView iOS 15弹出 Child NavigationView iOS 15 时的透明 TabBar
【发布时间】:2021-11-26 17:51:06
【问题描述】:

我有一个带有 TabBar 的 SwiftUI 应用程序。 如果我从 NavigationView 打开详细子视图,然后单击“返回”,TabBar 将变得透明,在 TabBar 图标下方显示 Feed 中的项目。

  1. 从主页,打开子导航详细视图。 ---------------

  1. 然后进入详细视图后,单击返回。 ---------------

  1. 你会看到这个错误。标签栏将是透明的。 ---------------

【问题讨论】:

    标签: ios swift swiftui uitabbarcontroller ios15


    【解决方案1】:

    在 iOS 15 中,Apple 已将对 scrollEdgeAppearance 的支持扩展到 UIKit。 默认情况下,此设置会生成透明的 TabBar 背景。

    要解决此问题,请将以下代码添加到您的 SceneDelegate 文件中,以定义 TabBar 的颜色,因此 SwiftUI 不会自动使其透明。

    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    var window: UIWindow?
    
    func scene(
        _ scene: UIScene,
        willConnectTo session: UISceneSession,
        options connectionOptions: UIScene.ConnectionOptions
    ) {
    
        guard let windowScene = (scene as? UIWindowScene) else { return }        
    
        // MARK: ADD THIS CODE BELOW TO YOUR SCENE DELEGATE.
        
        // TAB BAR BACKGROUND COLOR HERE.
        UITabBar.appearance().barTintColor = UIColor.white
        
        // TAB BAR ICONS COLOR HERE.
        UITabBar.appearance().tintColor = UIColor.blue
        UITabBar.appearance().isTranslucent = true
        
        if #available(iOS 15.0, *) {
            let appearance = UITabBarAppearance()
            appearance.configureWithOpaqueBackground()
            
            // TAB BAR BACKGROUND COLOR HERE. (same as above)
            appearance.backgroundColor = UIColor.white
            UITabBar.appearance().standardAppearance = appearance
            UITabBar.appearance().scrollEdgeAppearance = UITabBar.appearance().standardAppearance
        }
        
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: RootView())
            self.window = window
            window.makeKeyAndVisible()
        }
    }
    

    【讨论】:

    • 对我来说关键是 scrollEdgeAppearance。需要在iOS15中设置。很棒的发现,感谢分享!
    猜你喜欢
    • 2021-11-19
    • 2014-01-25
    • 2021-11-05
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    相关资源
    最近更新 更多