Changing of UISwitch text in iOS 4.2

前端 未结 5 957
名媛妹妹
名媛妹妹 2020-12-11 20:18

I am trying to change the text in UISwitch. The sample from the website ( Changing the text on a UISwitch ) works fine but when I upgrade my xcode to 3.2.5 and iOS 4.2, the

相关标签:
5条回答
  • 2020-12-11 21:17

    Don't do this. Manipulating the private view hierarchy of framework controls is absolutely unsupported, and can cause incompatibility with OS updates. Especially as your code does absolutely no verification of the hierarchy, so you can very easily crash if the number or type of subviews doesn't match what you were expecting.

    0 讨论(0)
  • 2020-12-11 21:17

    There still doesn't seem to be a supported way to change the TEXT yet, but you can change what is displayed by setting the onImage and offImage properties, introduced in iOS 6

    http://developer.apple.com/library/ios/documentation/uikit/reference/UISwitch_Class/Reference/Reference.html

    0 讨论(0)
  • 2020-12-11 21:19

    I found success in 4.2 with this code from here. It subclasses UISlider (not UISwitch) to achieve an effect that looks the same as a customized UISwitch.

    0 讨论(0)
  • 2020-12-11 21:21

    The latest version of XCode Beta 4 just added support for changing text on UISwitch. And also a SwtichStyle that I haven't investigated just what styles are out there.

    0 讨论(0)
  • 2020-12-11 21:22

    Try using a customizable open source UISwitch replacement.

    This one seems pretty good: DCRoundSwitch on GitHub, but there are many more on GitHub if that one doesn't work.

    This type of approach doesn't have the same incompatibility issues because it implements the switch's functionality and drawing itself and only relies on the core functionality UIControl base class.  The only thing you might need to update from time to time is the library you're using (DCRoundSwitch), however, base functionality like UIControl and Quartz drawing are quite established and rarely change so this can safely be considered a rare and minimal risk factor.

    0 讨论(0)
提交回复
热议问题