首页 > 解决方案 > 初始颜色设置不正确(Swift UI)

问题描述

我有Color来自 Swift UI 的库作为我的视图状态的一部分。我在初始化视图时尝试设置视图的颜色,但颜色设置不正确。

这是我的代码:

struct CalcButtonView: View {
    var calcButton: CalcButton
    var highlightAble: Bool = false
    @State var foregroundColor: Color? = nil
    @State var backgroundColor: Color? = nil
    @State private var highlighted: Bool = false

    @EnvironmentObject var numObservable: NumObservable

    init(newCalcButton: CalcButton){

        /// Buttons that are light gray
        let lightGrayButtons: [String] = ["C", "+/-", "%"]
        /// Buttons that are orange
        let orangeButtons: [String] = ["/", "x", "-", "+", "="]

        self.calcButton = newCalcButton
        self.highlighted = false
        self.foregroundColor = Color.white
        if lightGrayButtons.contains(self.calcButton.label){
            self.backgroundColor = Color.lightGray
            self.highlightAble = false
        } else if orangeButtons.contains(self.calcButton.label){
            self.backgroundColor = Color.orange
            self.highlightAble = true
        } else {
            self.backgroundColor = Color.darkGray
            self.highlightAble = false
        }
    }

}

结果是,当我的前景被明确设置为白色而我的背景颜色不应该是黑色时,我的所有背景颜色都恢复为蓝色,前景色为蓝色。

我认为这与我的 Color 结构设置为nil. 想法?

根据要求,这是我的body实现:

    var body: some View {
        Button(action: {
            self.numObservable.updateOrOperationDelegate(buttonLabel: self.calcButton.label)
            self.toggleHighLight()
        }, label:{
            Text(calcButton.label)
                .background(self.backgroundColor)
                .foregroundColor(self.foregroundColor)
                .cornerRadius(75)
        })
    }

更新#2:经过更多测试,我发现我的变量即使在我的函数运行foregroundColorbackgroundColor仍然存在。有谁知道怎么可能?nilinit

标签: swiftswiftui

解决方案


所以我找到了解决方案。我知道为什么会这样,但从技术上讲我不知道为什么会这样。我认为这与 Swift 处理@State. 我认为我的原始代码不起作用的原因是因为@State还没有准备好设置。

一旦我将代码移到初始化部分之外,它就起作用了。这是实现我最初目标的代码:

struct CalcButtonView: View {

    var calcButton: CalcButton
    var highlightAble: Bool
    @State var buttonForeground: Color = Color.white
    @State var buttonBackGround: Color?
    @State private var highlighted: Bool = false

    @EnvironmentObject var numObservable: NumObservable

    init(newCalcButton: CalcButton){
        let orangeButtons: [String] = ["/", "x", "-", "+", "="]

        self.highlightAble = false
        self.calcButton = newCalcButton
        self.highlighted = false
        if orangeButtons.contains(self.calcButton.label){
            self.highlightAble = true
        }

    }

    var body: some View {
        Button(action: {
            self.numObservable.updateOrOperationDelegate(buttonLabel: self.calcButton.label)
            self.toggleHighLight()
        }, label:{
            Text(calcButton.label)
                .background(self.buttonBackGround)
                .foregroundColor(self.buttonForeground)
                .cornerRadius(75)
        }).onAppear {
            /// Buttons that are light gray
            let lightGrayButtons: [String] = ["C", "+/-", "%"]
            /// Buttons that are orange
            let orangeButtons: [String] = ["/", "x", "-", "+", "="]
            if lightGrayButtons.contains(self.calcButton.label){
                self.buttonBackGround = Color.lightGray
            } else if orangeButtons.contains(self.calcButton.label){
                self.buttonBackGround = Color.orange
            } else {
                self.buttonBackGround = Color.darkGray
            }
        }
    }
}

更改在该.onAppear部分中。我所做的只是将我的background逻辑foreground移到.onAppear. 如果有人能告诉我为什么这有效,那么我将不胜感激。


推荐阅读