自定义UISwitch与所有iOS版本兼容

时间:2013-05-27 12:55:54

标签: ios uiswitch

我想为项目制作自定义UISwitch。我想改变文字和背景。

我在堆栈溢出上经历了多个帖子。一些只是图像绘制或核心图形绘制。我不想要他们两个,因为他们还不够。

  1. Custom UISwitch & App Store approval
  2. UISwitch - change from on/off to yes/no
  3. http://www.catamount.com/blog/uicustomswitch-customizing-uiswitch-color-it-change-labels/
  4. 但我需要一个代码,足以支持 iOS 4.2以及iOS 6.0。

2 个答案:

答案 0 :(得分:2)

使用此选项可更改背景颜色。这会更改UISwitch控件的关闭颜色

    // Set the tint color for the On state. Here we set green colour tint for On state
[customizedSwitch setOnTintColor:[UIColor colorWithRed:64.0/255 green:128.0/255.0 blue:64.0/255.0 alpha:1.0]];

// Set the tint color for the Off state. Here we set green red tint for Off state
[customizedSwitch setTintColor:[UIColor colorWithRed:255.0/255 green:128.0/255.0 blue:128.0/255.0 alpha:1.0]];

// Set the tint color for the round shaped Thumb. Here we set blue tint for the Thumb
[customizedSwitch setThumbTintColor:[UIColor colorWithRed:64.0/255 green:64.0/255.0 blue:255.0/255.0 alpha:1.0]];

注意:修改框架控件的私有视图层次结构是绝对不受支持的,如果您尝试使用uiswitch的子视图更改文本,则可能导致与OS更新不兼容。

将这个漂亮的教程用于这两项任务:customizing-user-interface-uiswitch 希望它可以帮到你。

答案 1 :(得分:1)

每当我需要一些开源组件时,我会转到cocoacontrols:也许这一个https://www.cocoacontrols.com/controls/ssswitch(我还没试过......)