[英]how to use schemes to change color of a ui element? (Xcode, swift)
我有五個不同的方案:調試,發布,登台,生產,測試。 我已經設置好了。
我想根據指定的方案更改標簽的文本和視圖的背景顏色。
在AppDelegate
我實現了以下方法:
func setDomainEnv() {
#if Debug
serverEndPointURL = "www.debug.com"
var fancyBackgroundColor = UIColor(hue: 0.007, saturation: 0.589, brightness: 0.572, alpha: 1.0)
#elseif Testing
serverEndPointURL = "www.testing.com"
var fancyBackgroundColor = UIColor(hue:0.971, saturation:0.715, brightness:1, alpha:1)
#elseif Staging
serverEndPointURL = "www.staging.com"
var fancyBackgroundColor = UIColor(hue: 0.373, saturation: 0.602, brightness: 0.863, alpha: 1.0)
#elseif Release
serverEndPointURL = "www.release.com"
var fancyBackgroundColor = UIColor(hue: 0.571, saturation: 1.0, brightness: 1.0, alpha: 1.0)
#elseif Production
serverEndPointURL = "www.production.com"
var fancyBackgroundColor = UIColor(hue: 0.66, saturation: 0.516, brightness: 0.871, alpha: 1.0)
#endif
}
在application didFinishLaunchingWithOptions
方法中調用該方法:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.setDomainEnv()
return true
}
我還建立了一個名為Constants
的文件,用於存儲我的變量:
import Foundation
import UIKit
var serverEndPointURL = "www.apple.com"
var fancyBackgroundColor = UIColor.black
在我的ViewController
,我在viewDidLoad
方法中使用這些變量:
override func viewDidLoad() {
super.viewDidLoad()
label.text = serverEndPointURL
self.view.backgroundColor = fancyBackgroundColor
}
但是,唯一更改的是標簽的文本。 背景顏色不會根據指定的方案而改變。 它始終為黑色(該值存儲在Constants
文件中)。 它不會根據所選方案而更改。 有任何想法嗎?
您正在定義一個新變量fancyBackgroundColor,而不是更改常量文件中已經存在的fancyBackgroundColor的值。 您應該在setDomainEnv方法中刪除var關鍵字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.