【问题标题】:Custom UISwitch Compatible with all versions of iOS自定义 UISwitch 兼容所有版本的 iOS
【发布时间】:2013-05-22 08:12:24
【问题描述】:

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

为此,我浏览了有关 stack-overflow 的多个帖子。有些只是图像绘制或核心图形绘制。我不想要它们,因为它们还不够。

  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/

但我需要一个代码,足以满足 iOS 4.2 和 iOS 6.0 的需求。

【问题讨论】:

    标签: ios uiswitch


    【解决方案1】:

    使用它来改变背景颜色。这会改变 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 的子视图更改文本,可能会导致与操作系统更新不兼容。

    使用这个很好的教程来完成这两个任务:customizing-user-interface-uiswitch 希望对你有帮助。

    【讨论】:

    • 他要求提供适用于 iOS 4.2 的代码。 setOnTintColor是 iOS 5.0+,thumbTintColor是 iOS 6.0+
    • 在 iOS 6.0 上如何在不使用图片的情况下设置标题?
    • 你可以使用这个:((UILabel *)[[[[[[myswitch subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:0]).text = @"YES"; ((UILabel *)[[[[[[myswitch subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:1]).text = @"NO";
    • @Nishant:我在 iOS 6.0 上使用过它,但它崩溃了,因为它无法获取 objectAtIndex:2。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2012-09-06
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      相关资源
      最近更新 更多