swift - 初始颜色设置不正确(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:经过更多测试,我发现我的变量即使在我的函数运行foregroundColor
后backgroundColor
仍然存在。有谁知道怎么可能?nil
init
解决方案
所以我找到了解决方案。我知道为什么会这样,但从技术上讲我不知道为什么会这样。我认为这与 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
. 如果有人能告诉我为什么这有效,那么我将不胜感激。
推荐阅读
- pandas - 如何按月顺序对我的数据进行排序?我希望我的月订单是 4 月到 4 月(即财政年度)
- ember.js - 我们不能在 glimmer 组件 getter 中放置一个 ember 并发任务,它正在跟踪组件参数
- git - Cygwin Symlinks .lnk 在 git clone 后不起作用
- artifactory - artifactory 不能以非法的 repo 名称开头
- laravel - Laravel 保护子对象的策略
- swift - 基于错误的枚举关联值,使用多个捕获和条件进行 Swift 错误处理
- sitemap - 如何在 Kentico Cloud 中管理站点地图?
- selenium-webdriver - 我正在尝试将值/文本发送到一个窗口,该窗口可通过 chrome 浏览器上的 Ctrl+F 键盘操作获得
- c# - 如何创建多语言低级全局键盘挂钩 c#
- c++ - 为什么“unsigned int”+“unsigned int”会返回“unsigned int”?