SwiftUI如何关闭iOS 15中TabView的背景透明效果
解决iOS 15 TabView(TabBar)背景透明问题
iOS 15 对UITabBar的默认交互样式做了调整,新增了scrollEdgeAppearance属性,当页面内的可滚动容器(如List、UITableView)内容未滚动到TabBar所在的底部区域时,TabBar会自动触发滚动边缘样式,默认呈现透明效果,对应效果如下:
你可以按照自己的技术栈选择对应的解决方式:
UIKit 项目解决方式
直接统一TabBar的标准样式和滚动边缘样式即可:
// Swift 版本 if #available(iOS 15.0, *) { let appearance = UITabBarAppearance() // 用默认带背景的配置,也可以自定义背景色、磨砂效果等 appearance.configureWithDefaultBackground() // 同时赋值给两种状态的外观,保证滚动到任何位置样式一致 UITabBar.appearance().standardAppearance = appearance UITabBar.appearance().scrollEdgeAppearance = appearance }
// Objective-C 版本 if (@available(iOS 15.0, *)) { UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init]; [appearance configureWithDefaultBackground]; [UITabBar.appearance() setStandardAppearance:appearance]; [UITabBar.appearance() setScrollEdgeAppearance:appearance]; }
如果只需要修改单个页面的TabBar样式,替换全局的UITabBar.appearance()为当前TabBarController的tabBar属性即可。
SwiftUI 项目解决方式
在App入口的初始化方法中修改全局TabBar外观即可:
@main struct MyApp: App { init() { if #available(iOS 15.0, *) { let appearance = UITabBarAppearance() appearance.configureWithDefaultBackground() // 自定义背景色可以加这行 // appearance.backgroundColor = .white UITabBar.appearance().scrollEdgeAppearance = appearance } } var body: some Scene { WindowGroup { ContentView() } } }
内容的提问来源于stack exchange,提问作者cole
相关产品推荐
相关产品推荐

