更改UISwitch的标签 [英] Change Label of UISwitch

查看:223
本文介绍了更改UISwitch的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将 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 7中,此属性无效。
在iOS 6中,此图像代表交换机的内部内容。您指定的图像与开关的圆形边框和拇指合成以创建最终外观。

In iOS 7, this property has no effect. In iOS 6, this image represents the interior contents of the switch. The image you specify is composited with the switch’s rounded bezel and thumb to create the final appearance.

并且它尚未标记为已弃用。不幸的是,在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的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆