简体   繁体   English

UISwitch - 从开/关切换到是/否

[英]UISwitch - change from on/off to yes/no

does anyone know of a way I can change the text label for on and off to yes and no. 有没有人知道我可以改变开启和关闭文本标签的方式是和否。

I did it with 我做到了

            ((UILabel *)[[[[[[switchControl subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:0]).text = @"Yes";
        ((UILabel *)[[[[[[switchControl subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:1]).text = @"No";

However, with the release of iOS 4.2, this is no longer supported (this probably wasn't recommended by Apple anyway) 但是,随着iOS 4.2的发布,不再支持此功能(这可能不是Apple推荐的)

My client is insisting on yes/no switches. 我的客户坚持使用是/否开关。 I'd appreciate any advice! 我很感激任何建议!

many thanks 非常感谢

Hurrah! 欢呼! From iOS 6, it's possible to specify an image to be used for the on / off states, respectively. 从iOS 6开始,可以分别指定用于开/关状态的图像。 So, this can be used to display a YES / NO image (or whatever image representing the text you would prefer to use instead of the previously limited ON / OFF). 因此,这可用于显示YES / NO图像(或表示您希望使用的文本的任何图像,而不是之前限制的ON / OFF)。

 if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0"))
 {
     [mySwitch setOnImage: [UIImage imageNamed:@"UISwitch-Yes"]];
     [mySwitch setOffImage:[UIImage imageNamed:@"UISwitch-No"]];
 }

The images should be 77 px wide, 27 px high, and the text (one image for each state) should be horizontally centred within that 77 px width. 图像应为77像素宽,27像素高,文本(每个状态一个图像)应在77像素宽度内水平居中。 I use transparent backgrounds for the text, so I can still make use of the tint for the background, which still works with this. 我为文本使用透明背景,所以我仍然可以使用背景的色调,这仍然适用于此。

Of course, it would seem easier to just supply text, rather than having to use an image of text, but I'm certainly grateful for this new option, at least. 当然,仅仅提供文本似乎更容易,而不是必须使用文本图像,但我当然感谢这个新选项,至少。

You need to implement your custom UISwitch for that. 您需要为此实现自定义UISwitch。 Or use one of already implemented :) (check this SO question and this post ) 或使用已经实现的:):检查这个问题这篇文章

Vladimir answer is great, but in my humble opinion there is an even better implementation here: https://github.com/domesticcatsoftware/DCRoundSwitch . 弗拉基米尔的答案很棒,但在我看来,这里有更好的实现: https//github.com/domesticcatsoftware/DCRoundSwitch

Besides setting a custom text, it is easier to change the size and color of the UISwitch and you get a sharper result. 除了设置自定义文本外,更容易更改UISwitch的大小和颜色,您可以获得更清晰的结果。

It is released under an MIT license. 它是在MIT许可下发布的。 Have a look! 看一看!

It turns out that you can create a custom UISwitch with the following items: 事实证明,您可以使用以下项创建自定义UISwitch:

  • A UIScrollView UIScrollView
  • A UIButton 一个UIButton
  • Two UILabels 两个UILabels
  • A background image 背景图像
  • A Boolean value 布尔值

First you will need to add QuartzCore.framework to your project and #import <QuartzCore/QuartzCore.h> to your view controller. 首先,您需要将QuartzCore.framework添加到项目中,并将#import <QuartzCore/QuartzCore.h>到视图控制器中。

Next add the UIScrollView to your view using Interface Builder. 接下来,使用Interface Builder将UIScrollView添加到视图中。 The ScrollView will be your custom UISwitch. ScrollView将是您的自定义UISwitch。 Next add the button and the two labels to your ScrollView. 接下来,将按钮和两个标签添加到ScrollView。 One label will be for "yes" the other for "no". 一个标签用于“是”,另一个用于“否”。

Add the image to the button and set its type to custom. 将图像添加到按钮并将其类型设置为自定义。 This is the image I use: 这是我使用的图像: 在此输入图像描述

Position the labels over the blue and white area of the image. 将标签放在图像的蓝色和白色区域上。 Adjust the ScrollView so it is just big enough to show the blue part of the image and the thumb nob. 调整ScrollView,使其足够大,以显示图像的蓝色部分和拇指按钮。

Add the following line to viewDidLoad: 将以下行添加到viewDidLoad:

self.mySwitch.layer.cornerRadius = 13.5;

mySwitch is the name of the ScrollView and 13.5 is half the height of the ScrollView. mySwitch是ScrollView的名称,13.5是ScrollView的一半高度。 The above statement changes the ScrollView to have rounded ends like the UISwitch. 上面的语句将ScrollView更改为具有圆形末端,如UISwitch。

To make the custom switch active you will need to tie the buttons "Touch Up Inside" event to an IBAction. 要使自定义开关处于活动状态,您需要将“Touch Up Inside”事件按钮绑定到IBAction。 Here is the code I use in the event handler: 这是我在事件处理程序中使用的代码:

-(IBAction)mySwitchButton:(id)sender {
    self.myValue = !self.myValue;
    CGPoint scrollPoint = CGPointMake((self.myValue)? 43.0: 0, 0.0);
    [mySwitch setContentOffset:scrollPoint animated:YES];
}

Where myValue is the boolean variable that contains the state of your switch and 43.0 is the number of points you will have to move the image over to put the switch in the off position. 其中myValue是包含开关状态的布尔变量,43.0是您必须移动图像以将开关置于关闭位置的点数。

That is all there is to it! 这就是它的全部!

From iOS 6, it's possible to specify an image to be used for the UISwitch on / off states, but NOT the text. 从iOS 6开始,可以指定用于UISwitch开/关状态的图像,但不能指定文本。 This will lead trouble when internationalization is required because translators have to provide an image text for each language, not text only. 当需要国际化时,这将导致麻烦,因为翻译人员必须为每种语言提供图像文本,而不是仅提供文本。 Moreover, the size of the UISwitch image is fixed, limiting the text length. 此外,UISwitch图像的大小是固定的,限制了文本长度。

Because of the above reasons, I like the JSWilson's answer: simple and flexible. 由于上述原因,我喜欢JSWilson的答案:简单灵活。

To relieve developers of the need to manually add the required controls, I coded a custom CRDScrollSwitch class that you can find at my GitHub repository: https://github.com/corerd/CRDScrollSwitch 为了减轻开发人员手动添加所需控件的需要,我编写了一个自定义CRDScrollSwitch类,您可以在我的GitHub存储库中找到它: https//github.com/corerd/CRDScrollSwitch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM