13

大家好。我正在创建一个简单的 SwiftUI 应用程序,我希望我的应用程序的 TabView 具有自定义背景而不是半透明的。

为了实现这一点,我使用UITabBar.appearance().backgroundColor = Colorand UITabBar.appearance().isTranslucent = false,它应该完全做到这一点,是的,它使栏不是半透明的,但不是为栏提供我选择的颜色,而是在选项卡栏顶部生成一个新视图t应该在那里,显然以前不在那里。

不改变标签栏的透明度和颜色 不改变标签栏颜色

更改标签栏的透明度和颜色 更改标签栏颜色

您可以注意到出现的新视图。我想这是一个问题,isTranslucent因为当我删除它时,新视图就消失了。

有没有办法可以更改颜色并使条不半透明并且不显示该视图?

任何帮助表示赞赏。提前致谢。

我的代码

SceneDelegate(仅变色部分)

UITabBar.appearance().isTranslucent = false
UITabBar.appearance().backgroundColor = UIColor(named: "backgroundColor")

选项卡视图

struct TabController: View {
    @State private var selection = 0

    var body: some View {
        TabView(selection: $selection) {
            HomePageView()
                .tabItem {
                    Image(systemName: "house.fill")
                        .font(.title)
                }
                .tag(0)

            Text("Second View")
                .font(.title)
                .tabItem {
                    Image(systemName: "bell.fill")
                        .font(.title)
                }
                .tag(1)
        }
            .edgesIgnoringSafeArea(.top)
    }
}
4

4 回答 4

15

您可以使用此代码设置标签栏颜色。把这段代码写在SceneDelegate

UITabBar.appearance().shadowImage = UIImage()
UITabBar.appearance().backgroundImage = UIImage()
UITabBar.appearance().isTranslucent = true
UITabBar.appearance().backgroundColor = .black

在 TabBar 背景中,您可以设置任何其他颜色而不是黑色。它工作得很好。

内容视图:

TabView(selection: $selection) {
        Text("1st View")
            .tabItem {
                Image(systemName: "house.fill")
                    .font(.title)
            }
            .tag(0)

        Text("Second View")
            .font(.title)
            .tabItem {
                Image(systemName: "bell.fill")
                    .font(.title)
            }
            .tag(1)
    }
        .edgesIgnoringSafeArea(.top)

预览:

于 2019-10-04T07:31:25.237 回答
7

这是正确的做法。

它也适用于 SwiftUI,因为 TabView 和 NavigationView 实际上是旧版 UITabBarController 和 UINavigationController 的 UIHostedController。

编辑:刚刚观看了 iOS 13 的现代化 UI 这就是这样做的方法:

let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor  .white]

然后在各种外观上设置外观。

navigationBar.standardAppearance = appearance
navigationBar.compactAppearance = appearance
navigationBar.scrollEdgeAppearance = appearance

参考:https ://developer.apple.com/videos/play/wwdc2019/224/

第二次编辑:需要一种从 SwiftUI 视图中访问 UINavigationController 的干净方法。

同时,这将有助于:

extension UINavigationController {
    override open func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        navigationBar.standardAppearance = appearance
        navigationBar.compactAppearance = appearance
        navigationBar.scrollEdgeAppearance = appearance
    }
}

extension UITabBarController {
    override open func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let appearance = UITabBarAppearance()
        appearance.configureWithOpaqueBackground()
        tabBar.standardAppearance = appearance
    }
}
于 2019-10-01T16:09:35.230 回答
2

您可以访问 UINavigationController 或 UITabbarController 来使用 [https://github.com/siteline/SwiftUI-Introspect]

像这样写

NavigationVIew {...}
.introspectNavigationController { (navController) in
    let coloredAppearance = UINavigationBarAppearance()
    coloredAppearance.configureWithOpaqueBackground()
    
    coloredAppearance.backgroundColor = backgroundColor
    coloredAppearance.backgroundImage?.withTintColor(backgroundColor)
    coloredAppearance.titleTextAttributes = [.foregroundColor: tintColor]
    coloredAppearance.largeTitleTextAttributes = [.foregroundColor: tintColor]
    coloredAppearance.shadowColor = shadowColor
    
    navController.navigationBar.compactAppearance = coloredAppearance
    navController.navigationBar.standardAppearance = coloredAppearance
    navController.navigationBar.scrollEdgeAppearance = coloredAppearance
    navController.navigationBar.tintColor =  tintColor
}
于 2021-01-07T05:50:51.450 回答
2

您可以使用这样的扩展:

extension UITabBarController {
    override open func viewDidLoad() {
        super.viewDidLoad()
        let appearance = UITabBarAppearance()
        appearance.backgroundColor = .black
        tabBar.standardAppearance = appearance
    }
}

请注意,被覆盖的函数必须是viewDidLoad(). 至少当它是一个viewDidAppear(:)函数时它对我不起作用。

于 2021-03-09T16:43:01.127 回答