xcode 正确自定义uiswitch图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16446602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:21:58  来源:igfitidea点击:

Customize uiswitch image properly?

iosxcodecustomizationuiswitch

提问by Tom

I have an UISwitch in my iOS 6 app, that's on and off image is customised.

我的 iOS 6 应用程序中有一个 UISwitch,它的开启和关闭图像是自定义的。

self.testSwitch.onImage = [UIImage imageNamed:@"on"]; 
self.testSwitch.offImage = [UIImage imageNamed:@"off"];

I use 77 points wide and 22 points tall image for this purpose (154x44 in retina), as it is stated in the documentation. But my image does not fit to my uiswitch, it seems ugly, the default style hides my one, like on the attached image.

为此,我使用 77 点宽和 22 点高的图像(视网膜中为 154x44),如文档中所述。但是我的图像不适合我的 uiswitch,它看起来很丑陋,默认样式隐藏了我的图像,就像附加的图像一样。

enter image description here

在此处输入图片说明

What should I set to make it work in a proper way?

我应该设置什么才能使其以正确的方式工作?

采纳答案by tobs

Apple doesn't have an Appearance API for UISwitch. You can set a tint color property (onTintColor). But that's not what you want, I guess. The problem about customizing UISwitch is that there's the opportunity that Apple will reject your app.

Apple 没有 Appearance API 用于UISwitch. 您可以设置色调颜色属性 ( onTintColor)。但这不是你想要的,我猜。自定义 UISwitch 的问题在于 Apple 可能会拒绝您的应用程序。

But there are some APIs for a custom switch such as RCSwitch(https://github.com/robertchin/rcswitch) or TTSwitch. A good tutorial and example of how to use RCSwitchcan be found here: http://www.raywenderlich.com/23424/photoshop-for-developers-creating-a-custom-uiswitch.

但是有一些用于自定义开关的 API,例如RCSwitch( https://github.com/robertchin/rcswitch) 或TTSwitch. 一个很好的教程和如何使用的例子 RCSwitch可以在这里找到:http: //www.raywenderlich.com/23424/photoshop-for-developers-creating-a-custom-uiswitch

回答by matt

Here is code from my book. This is not exactly what you want to do, but it shows the technique and will get you started! Notice that I'm using 79 by 27 (not sure where you got your numbers from):

这是我书中的代码。这并不完全是您想要做的,但它展示了技术并会让您开始!请注意,我使用的是 79 x 27(不确定您的数字是从哪里获得的):

UIGraphicsBeginImageContextWithOptions(CGSizeMake(79,27), NO, 0);
[[UIColor blackColor] setFill];
UIBezierPath* p = [UIBezierPath bezierPathWithRect:CGRectMake(0,0,79,27)];
[p fill];
NSMutableParagraphStyle* para = [NSMutableParagraphStyle new];
para.alignment = NSTextAlignmentCenter;
NSAttributedString* att =
    [[NSAttributedString alloc] initWithString:@"YES" attributes:
        @{
            NSFontAttributeName:[UIFont fontWithName:@"GillSans-Bold" size:16],
            NSForegroundColorAttributeName:[UIColor whiteColor],
            NSParagraphStyleAttributeName:para
        }];
[att drawInRect:CGRectMake(0,5,79,22)];
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.sw2.onImage = im;

Looks like this:

看起来像这样:

enter image description here

在此处输入图片说明