大家好。我正在创建一个简单的SwiftUI应用,我希望我的应用的TabView具有自定义背景并且不透明。
要实现此目的,我使用了UITabBar.appearance().backgroundColor = Color
和UITabBar.appearance().isTranslucent = false
,它们应该做到这一点,是的,它使条形不是半透明的,而是给条形赋予了我选择的颜色,而是在选项卡栏的顶部产生一个新视图,该视图本来不应该存在,而且显然不存在。
您可以注意到出现的新视图。我猜这是isTranslucent
的问题,因为当我删除它时,新视图消失了。
有没有办法改变颜色并使条形图不透明并且不显示该视图?
感谢您的帮助。预先感谢。
SceneDelegate (仅限不断变化的颜色部分)
UITabBar.appearance().isTranslucent = false
UITabBar.appearance().backgroundColor = UIColor(named: "backgroundColor")
TabView
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)
}
}
答案 0 :(得分:1)
这是正确的方法。
它也可以与SwiftUI一起使用,因为TabView和NavigationView实际上是旧版UITabBarController和UINavigationController的UIHostedController。
编辑:刚刚观看过iOS的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
}
}
答案 1 :(得分:1)
您可以使用这样的扩展程序:
extension UITabBarController {
override open func viewDidLoad() {
super.viewDidLoad()
let appearance = UITabBarAppearance()
appearance.backgroundColor = .black
tabBar.standardAppearance = appearance
}
}
注意被覆盖的函数必须是viewDidLoad()
。至少当它是 viewDidAppear(:)
函数时它对我不起作用。
答案 2 :(得分:0)
您可以使用此代码设置标签栏颜色。在SceneDelegate
UITabBar.appearance().shadowImage = UIImage()
UITabBar.appearance().backgroundImage = UIImage()
UITabBar.appearance().isTranslucent = true
UITabBar.appearance().backgroundColor = .black
在TabBar背景中,您可以设置任何其他颜色而不是黑色。工作正常。
ContentView:
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)
答案 3 :(得分:0)
您可以访问 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
}