Xcode 不支持使用 Schemes 进行条件编译。您需要为此维护两个目标,而这很快就会变得混乱。
要使用方案执行此操作,您需要维护两个具有正确命名颜色的资产目录,并在构建时复制正确的一个。源 .xcasset 目录不会添加到您的目标中。
您需要尽早在目标的“构建阶段”部分添加一个运行脚本。
幸运的是,Scheme 名称是通过 CONFIGURATION 环境变量表示的。你可以这样做,你的路径可能会有所不同:
# Copy over the appropriate asset catalog for the scheme
target=${SRCROOT}/Resources/Colors.xcassets
if [ "${CONFIGURATION}" = "Scheme 1" ]; then
sourceassets=${PROJECT_DIR}/Scheme1.xcassets
else
sourceassets =${PROJECT_DIR}/Scheme2.xcassets
fi
if [ -e ${target} ]; then
echo "Assets: Purging ${target}"
rm -rf ${target}
fi
echo "Assets: Copying source=${sourceassets} to destination=${target}"
cp -r ${sourceassets} ${target}
本质上,您是在用您的 Scheme 特定版本之一替换资产目录的编译版本。
字符串将是另一个问题,您可以使用相同的技术来处理本地化字符串。
这一切都很快变得可怕,不推荐。使用your referenced post 中描述的技术在运行时通过代码配置 UI 会更好。
您可以构建一个 shim 层来保护您的代码免受 Scheme 更改的影响。
例如
@interface MyColors : NSObject
+ (UIColor *)buttonBackground;
@end
@implementation MyColors
+ (UIColor *)buttonBackground {
#if SCHEME1
return [UIColor colorNamed:@"scheme1ButtonBackground"];
#else
return [UIColor colorNamed:@"scheme2ButtonBackground"];
#endif
}
@end