弹出子导航视图时透明TabBar iOS 15

Art*_*tto 10 uitabbarcontroller ios swift swiftui ios15

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

  1. 从主屏幕中,打开子导航详细信息视图。----------------

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述

小智 8

这就是我需要做的一切。

if #available(iOS 15.0, *) {
    let tabBarAppearance = UITabBarAppearance()
    tabBarAppearance.configureWithDefaultBackground()
    UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
}
Run Code Online (Sandbox Code Playgroud)


Art*_*tto 5

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