Customize uiswitch image correctly?

I have UISwitch in my iOS 6 app that is being configured on and off.

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

I use 77 dots in width and a 22-dot image for this purpose (154x44 in the retina), as indicated in the documentation. But my image is not suitable for my uiswitch, it seems ugly, the default style hides mine, as in the attached image.

enter image description here

What should I configure for it to work properly?

+4
source share
2 answers

Apple does not have an appearance API for UISwitch . You can set the hue color property ( onTintColor ). But that is not what you want, I think. The problem with configuring UISwitch is that Apple may reject your application.

But for the custom switch, there are some APIs like RCSwitch ( https://github.com/robertchin/rcswitch ) or TTSwitch . A good tutorial and an example of using RCSwitch can be found here: http://www.raywenderlich.com/23424/photoshop-for-developers-creating-a-custom-uiswitch .

+2
source

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

 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; 

It looks like:

enter image description here

+6
source

Source: https://habr.com/ru/post/1479792/


All Articles