【问题标题】:Initial Color is not being set properly (Swift UI)初始颜色设置不正确(Swift UI)
【发布时间】:2020-04-12 09:01:48
【问题描述】:

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

这是我的代码:

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 仍然是nil,即使在我的init 函数运行之后。有谁知道怎么可能?

【问题讨论】:

  • 你能告诉我们你的body实现吗?
  • @Palle 我添加了我的身体。

标签: swift swiftui


【解决方案1】:

所以我找到了解决方案。我知道为什么会这样,但从技术上讲我不知道为什么会这样。我认为这与 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部分中。我所做的只是将我的backgroundforeground 逻辑移动到.onAppear。如果有人能告诉我为什么这有效,那么我将不胜感激。

【讨论】:

    猜你喜欢
    • 2015-01-21
    • 1970-01-01
    • 2018-10-25
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    相关资源
    最近更新 更多