【问题标题】:Button switch change default color按钮开关更改默认颜色
【发布时间】:2013-12-24 06:49:57
【问题描述】:

如何更改 UISwitch 按钮的颜色?当它出现在模拟器中时,它不应该在On,而是在Off。我想看到它是白色的,当它被激活时,它应该变成绿色。

【问题讨论】:

    标签: ios uiswitch


    【解决方案1】:

    创建一个用于更改开关值的处理程序(见下文)。另外别忘了根据开关的状态设置初始颜色。

    - (void)onFlipSwitch:(UISwitch *)aSwitch {
        if(aSwitch.on) {
            aSwitch.backgroundColor = [UIColor greenColor];
        }
        else {
            aSwitch.backgroundColor = [UIColor whiteColor];
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以访问 UISwitchView 的 onTintColor 和 tintColor 属性。

      @property(nonatomic, retain) UIColor *onTintColor
      @property(nonatomic, retain) UIColor *tintColor
      

      初始化开关时,将 onTintColor 设置为您想要的颜色为 On 值,将 tintColor 设置为 Off 值。 例如在 UIViewController 中:

          UISwitch *theSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 41.0, 30.0)];
      
          // on color
          theSwitch.onTintColor = [UIColor blueColor];
      
          //off color
          theSwitch.tintColor = [UIColor redColor];
      
          [self.view addSubview:theSwitch];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-31
        • 2019-01-21
        • 2021-06-22
        • 2020-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多