自定义uiswitch图像?

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

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

我使用77点宽和22点高的图像(视网膜154×44),如文件中所述。 但是我的图片不适合我的uiswitch,看起来很丑,默认的风格隐藏了我的图片,就像附加的图片一样。

在这里输入图像说明

我应该怎样设置才能使其工作正常?

苹果公司没有UISwitch的外观API。 你可以设置一个色彩属性( onTintColor )。 但是,这不是你想要的,我想。 定制UISwitch的问题是,苹果会拒绝你的应用程序。

但是有一些自定义开关的API,如RCSwitchhttps://github.com/robertchin/rcswitch )或TTSwitch 。 一个很好的教程和如何使用RCSwitch可以在这里find: http : RCSwitch

这是我书中的代码 。 这不完全是你想要做的,但它显示了技术,并会让你开始! 请注意,我正在使用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; 

看起来像这样:

在这里输入图像说明