【问题标题】:Xcode Change color theme based on targetXcode 根据目标更改颜色主题
【发布时间】:2015-11-27 06:11:20
【问题描述】:

我正在开发一个 ipad 应用程序,它有两个目标,但目标的颜色主题会有所不同,例如在 Target1 中,选定的按钮字体颜色为红色,在 Target2 中,选定的按钮字体颜色为绿色。我想知道这在界面生成器本身中是否可行?

提前致谢!!

【问题讨论】:

    标签: ios xcode uibutton xcode7 targets


    【解决方案1】:

    您可以使用预处理器宏。选择您的目标,转到目标的 Build Settings 部分,找到 Preprocessor Marcro 并为每个目标添加 新宏(例如 Target1 为您的第一个目标和第二个目标的 Target2)。现在你可以使用代码了:

    #ifdef Target1
      //code for your first target
    #elif Target2
      //code for your second target
    #endif
    

    希望对你有帮助;-)

    【讨论】:

    • 如果我有数百个目标,那么这是不可扩展的。还有其他优雅的解决方案吗?
    【解决方案2】:

    你可以使用这个解决方案:

    为多个目标创建单个 .h 文件和多个 .m 文件。

    1. 使用您的应用主题创建 .h 文件

    //h 文件

    @interface AppTheme : NSObject 
    
    
    @property (nonatomic) UIColor *backgroundColor;
    @property (nonatomic) UIColor *buttonTextColor;
    //and so on.
    //you can create classes or protocols for curtain elements of your app
    //
    @property (nonatomic) ButtonStyle *defaultButtonStyle;
    @property (nonatomic) ButtonStyle *destroyButtonStyle;
    //and so on where 
    // Button style has titleColor, titleFont, background and ect.
    
    @end
    
    1. 现在您可以为目标 A 创建 .m 文件

    AppTheme+A.m 你可以像这样定义你的主题:

    @implementation
    
    - (instancetype)init {
    
       if (self = [super init]){
           self.backgroundColor = [UIColor black];
           self.buttonTextColor = [UIColor white];
    //and so on
       }
    
       return self;
    }
    
    @end
    

    对于目标 B,您可以创建其他文件: AppTheme+B.m 你可以像这样定义你的主题:

    @implementation
    
    - (instancetype)init {
    
       if (self = [super init]){
           self.backgroundColor = [UIColor pinkColor];
           self.buttonTextColor = [UIColor yellowColor];
    //and so on
       }
    
       return self;
    }
    
    @end
    
    1. 然后将您的 AppTheme+A.m 包含在目标 A 和 AppTheme+B.m 目标 B 中。

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 2021-01-30
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 2023-03-26
      • 2023-04-05
      • 2020-09-16
      • 1970-01-01
      相关资源
      最近更新 更多