【问题标题】:How to change the UISwitch default color(blue)如何更改 UISwitch 默认颜色(蓝色)
【发布时间】:2012-01-26 01:47:16
【问题描述】:

如何更改 UISwitch 的默认颜色(蓝色)?

【问题讨论】:

  • 现在(2019 年)只是名字不好的 .onTintColor 。请注意,这与通常的“tintColor”无关。

标签: ios iphone ios5 ios4


【解决方案1】:

我认为您正在寻找的是这样的东西

UISwitch *testSwitch; //just something I made up
[testSwitch setOnTintColor:[UIColor greenColor]];

【讨论】:

    【解决方案2】:

    在 iOS 5 之前,无需编写您自己的自定义 UISwitch 控件,可能使用 UISegmentedControl,Apple 不允许您更改标准 UISwitch 的颜色。

    有一个私有属性 setAlternateColor: YES 会将颜色更改为橙​​色,您需要为 UISwitch 类创建一个类别,但这不会在 Apple 中获得批准审查过程。

    以下是一些用于 iOS 3.0 - 4.1 的自定义 UISwitch 项目:

    1. http://osiris.laya.com/projects/rcswitch/
    2. http://www.alexcurylo.com/blog/2010/07/30/custom-uiswitch/
    3. StackOverflow 分析器:https://stackoverflow.com/a/5088099/171206(使用 UISegmentedControl

    在 iOS 5 中引入的UISwitch 现在有一个onTintColor 属性。

    [mySwitch setOnTintColor: [UIColor blackColor]];
    

    【讨论】:

      【解决方案3】:

      最后,在 iOS5 中,您可以使用属性 onTintColor 更改开关的颜色。

      UISwitch *s = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
      s.on = YES;
      s.onTintColor = [UIColor redColor];
      [self.view addSubview:s];
      [s release];
      

      制作这个

      希望对你有所帮助!

      【讨论】:

        【解决方案4】:

        在 Xcode 5 和 iOS 7 中,它现在位于属性检查器中:

        更改 On Tint 将更改按钮打开时的颜色。

        我希望这就是您想要的!即使您在三年前发布了这个问题。

        【讨论】:

          【解决方案5】:

          斯威夫特 3:

          yourSwitch.onTintColor = .red
          

          【讨论】:

            【解决方案6】:

            斯威夫特 3 斯威夫特 4

            可行的解决方案

            var switcher = UISwitch()
            switcher.onTintColor = .green
            switcher.tintColor = .green
            

            【讨论】:

              【解决方案7】:

              为特定的 UISwitch 设置 tint color:

              var switcher = UISwitch()
              switcher.onTintColor = .red
              switcher.tintColor = .red
              

              为您的应用设置色调:

              let switchApperence = UISwitch.appearance()
              switchApperence.tintColor = .red
              switchApperence.onTintColor = .red
              

              【讨论】:

              • 为什么要设置tintColor属性?只需设置 onTintColor,它就可以按预期为我工作。
              猜你喜欢
              • 1970-01-01
              • 2014-10-26
              • 2015-10-06
              • 1970-01-01
              • 2021-03-21
              • 1970-01-01
              • 2018-02-16
              • 1970-01-01
              • 2012-01-15
              相关资源
              最近更新 更多