问题描述
我必须将 UISwitch
的标签从ON-OFF更改为YES-NO。
I have to change the label of a UISwitch
from ON-OFF to YES-NO.
I希望这个方法在单独的类中实现,然后由其他类访问。
I want this method to be implemented in separate class and then accessed by other classes.
我试图实现烹饪书中提供的片段,但没有成功
I have tried to implement the snippets provided in the cook book, but without success
推荐答案
UISwitch
使用图片进行绘图。要更改 UISwitch
的文本,您必须设置 onImage
和 offImage
UISwitch
的属性,以便将图像与自定义文本一起使用。这可以直接在 UISwitch
实例上完成,或者使用 UIAppearance
在所有<$ c $中设置自定义图像c> UISwitch 您应用中的实例:
UISwitch
uses images for drawing. To change the text of a UISwitch
, you would have to set the onImage
and offImage
properties of the UISwitch
to use images with your custom text. This could be done directly on a UISwitch
instance, or using UIAppearance
to set your custom image across all UISwitch
instances in your app:
[[UISwitch appearance] setOnImage:onImage];
[[UISwitch appearance] setOffImage:offImage];
不幸的是,在iOS 7或更高版本中,为UISwitch设置自定义开启和关闭图像功能不起作用。来自:
Unfortunately, setting custom on and off images for UISwitch is not functional in iOS 7 or later. From the documentation:
并且它尚未标记为已弃用。不幸的是,在iOS 8中,情况似乎仍然如此。自定义 UISwitch
的颜色仍然有效,但使用自定义图像则不然。要自定义开关的图像(以及文本),您必须使用自定义控件类。
And it has not been marked as deprecated. In iOS 8 this still seems to be the case, unfortunately. Customizing the colors of a UISwitch
still works, but using custom images does not. To customize the images (and thus text) of a switch you will have to use a custom control class.
这篇关于更改UISwitch的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!