Swiftui 更改选项卡式视图栏颜色快捷界面
有人知道如何更改选项卡式视图底部栏的背景颜色吗 我已经设置了重点颜色,当我选择每个选项卡栏项目时,它改变了我图标的颜色 我尝试过将背景设置为一种颜色,但它不会改变背景,并且尝试将背景设置为一个图像,只是为了确保这一点,但这也没有任何作用Swiftui 更改选项卡式视图栏颜色快捷界面,swiftui,Swiftui,有人知道如何更改选项卡式视图底部栏的背景颜色吗 我已经设置了重点颜色,当我选择每个选项卡栏项目时,它改变了我图标的颜色 我尝试过将背景设置为一种颜色,但它不会改变背景,并且尝试将背景设置为一个图像,只是为了确保这一点,但这也没有任何作用 想知道我是否需要以某种方式专门访问底部栏,然后在其上设置属性 这里有一个解决方案。您可以更改UITabBar的外观,并更改选项卡栏 struct TabView: View { init() { UITabBar.appearance()
想知道我是否需要以某种方式专门访问底部栏,然后在其上设置属性 这里有一个解决方案。您可以更改UITabBar的
外观
,并更改选项卡栏
struct TabView: View {
init() {
UITabBar.appearance().backgroundColor = UIColor.blue
}
var body: some View {
return TabbedView {
Text("This is tab 1").tag(0).tabItemLabel(Text("tab1"))
Text("This is tab 2").tag(1).tabItemLabel(Text("tab2"))
Text("This is tab 3").tag(2).tabItemLabel(Text("tab3"))
}
}
}
TabbedView已被弃用,现在您可以尝试:
struct AppTabbedView: View {
@State private var selection = 3
init() {
UITabBar.appearance().backgroundColor = UIColor.blue
}
var body: some View {
TabView (selection:$selection){
Text("The First Tab")
.tabItem {
Image(systemName: "1.square.fill")
Text("First")
}
.tag(1)
Text("Another Tab")
.tabItem {
Image(systemName: "2.square.fill")
Text("Second")
}.tag(2)
Text("The Last Tab")
.tabItem {
Image(systemName: "3.square.fill")
Text("Third")
}.tag(3)
}
.font(.headline)
}
}
在init()中添加uitabar.appearance().bartincolor=UIColor.blue
struct ContentView: View {
@State private var selection = 1
init() {
UITabBar.appearance().barTintColor = UIColor.blue
}
var body: some View {
TabView (selection:$selection){
Text("The First Tab")
.tabItem {
Image(systemName: "1.square.fill")
Text("First")
}
.tag(1)
Text("Another Tab")
.tabItem {
Image(systemName: "2.square.fill")
Text("Second")
}.tag(2)
Text("The Last Tab")
.tabItem {
Image(systemName: "3.square.fill")
Text("Third")
}.tag(3)
}
.accentColor(.white)
}
}
这一方案看起来像是基于最新版本的Swift和SwiftUI的有效解决方案
struct TabBar: View {
init() {
UITabBar.appearance().barTintColor = UIcolor.black
var body: some View {
TabView {
HomeView().tabItem {
Image(systemName: "house.fill")
Text("Home")
}
MapView().tabItem {
Image(systemName: "mappin.circle.fill")
Text("Map")
}
}
.edgesIgnoringSafeArea(.top)
}
}
其中HomeView()和MapView()只是先前创建的一些其他视图,将在点击时显示。SwiftUI 1.0-使用命名颜色
组合barTintColor
和isTranslucent
出于某种原因,当我使用barTintColor
甚至backgroundColor
时,我没有得到我命名颜色的全彩。我还必须包括isTranslucent
这是我命名的颜色:
仅设置BartinColor
(如您所见,它略微褪色)
仅设置背景色backgroundColor
(这会使条形图变暗一点)
将barTintColor
和isTranslucent
设置为False
正是这种组合为我做到了这一点:
UITabBar.appearance().isTranslucent = false
UITabBar.appearance().barTintColor = UIColor(named: "Secondary")
虽然这对于灯光模式很好,但当您切换到暗模式时,选项卡栏的背景将保持您选择的颜色。当暗模式为sl时,任何使条形图变为黑色的方法都只是警告,此答案依赖于SwiftUI使用
UITabBar
来表示选项卡视图
。这可能会在任何时候发生变化,例如,如果苹果公司创建了一个纯swift标签栏。在这种情况下,我会更新答案,或者有人会更新答案。嗨@mumu,我尝试过这样做,但不知怎的,它使标签颜色不透明。你有什么解决办法的建议吗?@grey-见下面的答案。uitabar.appearance().barTintColor=UIColor.blue这对我有效,但被接受的答案没有;我试着把背景调成白色,而接受的答案是灰色的(虽然比以前稍微轻一点)。谢谢