正确定制uiswitch图像?

Tom*_*Tom 4 xcode customization uiswitch ios

我的iOS 6应用程序中有一个UISwitch,可以自定义打开和关闭图像.

self.testSwitch.onImage = [UIImage imageNamed:@"on"]; 
self.testSwitch.offImage = [UIImage imageNamed:@"off"];
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

我应该设置什么才能使其正常工作?

mat*_*att 6

这是我书中的代码.这不是你想要做的,但它显示了这项技术,并将帮助你入门!请注意,我使用的是79乘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;
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

在此输入图像描述