我创建了一个带有一些按钮的网格,每个按钮都与某种颜色相关联,并改变了视图的背景颜色:
@IBOutlet弱var backgroundSelectionView:UIView! 覆盖函数…
我没有看到其他按钮以及您更改背景颜色的位置但我们假设您正在使用颜色ypu的HeX represantation可以在共享首选项中保存背景颜色并始终使用共享状态来设置背景。
的 当您想将颜色保存到用户首选项时 强> : UserDefaults.standard.set("#FF2200", forKey: "background_color_hex")
UserDefaults.standard.set("#FF2200", forKey: "background_color_hex")
的 从prefs检索背景颜色的HEX represantation 强> : let hexBackgroundColor = UserDefaults.standard.string(forKey: ��background_color_hex��) ?? ��#FFFFFF�� 。
let hexBackgroundColor = UserDefaults.standard.string(forKey: ��background_color_hex��) ?? ��#FFFFFF��
然后你可以使用它 的 基于其十六进制represantation获取UIColor的扩展 强> 就像许多在线文章中所解释的那样,以此为例 https://www.hackingwithswift.com/example-code/uicolor/how-to-convert-a-hex-color-to-a-uicolor 像这样: view.backgroundColor = UIColor(hex: hexBackgroundColor)
view.backgroundColor = UIColor(hex: hexBackgroundColor)
我想可以有很多其他的解决方案,这是我想到的最快的解决方案。