我正在try 更改View的色调 colored颜色 .

我得到的是:

    var body: some View {
    Button {
        selectedTab = title
    } label: {
        VStack(alignment: .center) {
            image.renderingMode(.template)
            Text(title)
        }
        .foregroundColor(selectedTab == title ? .accentColor : .black.opacity(0.2))
        .padding()
    }
}

The problem: When I use .accentColor(Color) in superview for this subview, Xcode said: enter image description here

所以,我使用,就像在文档中:apple docs 101:

            if #available(iOS 15.0, *) {
            CustomTabView(tabs: "").tint(.red)
        } else {
            CustomTabView(tabs: "").accentColor(.green)
        }

强调色很好,但.tint不行.我哪里做错了?

enter image description here

enter image description here

推荐答案

您的代码不起作用,因为当您使用tint并 Select 选项卡时,选项卡的 colored颜色 会被foregroundColor修饰符覆盖.它会将选项卡的 colored颜色 设置为.accentColor,这与tint设置的 colored颜色 无关.但是,不推荐使用的accentColor修饰符设置了它,这就是为什么不推荐使用accentColor修饰符可以工作.

解决这个问题的一种方法就是不使用.accentColor作为前景色.改用nil:

.foregroundColor(selectedTab == title ? nil : .black.opacity(0.2))

当它为零时,它不会覆盖色调.

Swift相关问答推荐

如何将泛型函数存储到变量中?

按Esc键时执行操作(工作表)

如何在visionOS中进行购买?&# 39;购买(选项:)在visionOS中不可用

在SwiftUI中绘制形状的路径后填充形状

使用 @resultBuilder 的通用 buildList 函数

物理主体未与 spritekit Swift 的网格图案上的纹理图像对齐

ToolbarItem 包含在动画中但不应该包含在动画中

UIBezierPath() 和 DragGesture:连接点创建角而不是曲线

无法使用 RealityKit 播放 USDZ 动画

为什么 NumberFormatter 在 Swift 中将 19,999,999,999,999,999 格式化为 20,000,000,000,000,000?

您是否必须手动指定您的 DispatchQueue 是串行的?

Swift 全局函数列表

iOS SwiftUI - 无法转换 ObservedObject 类型的值

在 Swift 4 中实现自定义解码器

使用 DispatchTime.now() + float 延迟?

Swift 中的 CommonHMAC

Swift 2.0 按属性对对象数组进行排序

SwiftUI:将多个 BindableObjects 放入环境

应用程序包不包含有效标识符

Objective-C 方法与可选需求方法 Swift 冲突