gpt4 book ai didi

SwiftUI 标签栏颜色

转载 作者:行者123 更新时间:2023-12-01 10:05:43 25 4
gpt4 key购买 nike

如何设置标签栏颜色?例如,仅使用灰色条分配黑色结果。
这是针对 SwiftUI 的。
指定暗模式不是一个合适的解决方法。

Screen shot

    struct ContentView: View {

@State private var selection = 1



init() {
UITabBar.appearance().backgroundColor = UIColor.blue
UITabBar.appearance().backgroundImage = UIImage()
//UITabBar.appearance().isTranslucent = false
//UITabBar.appearance().shadowImage = UIImage()

}

var body: some View {

TabView {
ClockView()
.tabItem {
Image("clock")
Text("Clock")
}.tag(0)
PlanetsNowView()
.tabItem {
Image("clock")
Text("Now")
}.tag(1)
SettingsView()
.tabItem {
Image("settings")
Text("Settings")
}.tag(2)
}
.accentColor(.white)
.opacity(1)
//.environment(\.colorScheme, .dark)
}
}

最佳答案

这是在 SwiftUI View 中创建黑色标签栏的初始化程序。

import SwiftUI

struct ContentView: View {

init() {
setupTabBar()
}

var body: some View {
TabView {
//Your tab bar items
}
}
}

//MARK: - Tab bar view appearance
extension ContentView {
func setupTabBar() {
UITabBar.appearance().barTintColor = .black
UITabBar.appearance().tintColor = .blue
UITabBar.appearance().layer.borderColor = UIColor.clear.cgColor
UITabBar.appearance().clipsToBounds = true
}
}

如果要根据用户的亮/暗模式设置更改颜色:
  • 打开“Assets.xcassets”文件夹
  • 右键单击您的 Assets 面板
  • 选择“新颜色集”
  • 打开新颜色的属性检查器面板
  • 选择“外观”
  • 选择“任何,黑暗”

  • 您现在将有两个彩色方块,您必须为第一个选择亮模式颜色,为第二个选择暗模式颜色。

    要在初始化标签栏时在代码中使用它,请使用新的明/暗模式颜色集的名称更改定义 barTintColor 的行。
    UITabBar.appearance().barTintColor = UIColor(named: "<your color name>")

    关于SwiftUI 标签栏颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58249715/

    25 4 0