【问题标题】:how to use schemes to change color of a ui element? (Xcode, swift)如何使用方案来更改 ui 元素的颜色? (Xcode,迅速)
【发布时间】:2018-07-25 22:04:51
【问题描述】:

我有五种不同的方案:调试、发布、登台、生产、测试​​。我已经全部设置好了。

我想根据指定的方案更改标签的文本和视图的背景颜色。

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 文件中的值)。它不会根据所选方案而改变。有什么想法吗?

【问题讨论】:

  • 你在环境变量(stackoverflow.com/questions/29131865/…)中声明了ProductionRelease等吗?
  • 您是否在setDomainEnv() 中添加断点以查看是否触发了#ifs 中的任何一个?

标签: ios swift configuration uilabel


【解决方案1】:

您正在定义一个新变量 fancyBackgroundColor,而不是更改常量文件中已经存在的 fancyBackgroundColor 的值。您应该删除 setDomainEnv 方法中的 var 关键字。

【讨论】:

    猜你喜欢
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    相关资源
    最近更新 更多