Implementing a custom UISwitch using a library - what am I doing wrong that's causing the size issues?

StackOverflow https://stackoverflow.com/questions/17014433

Question

I'm using BMXSwitch to implement a custom UISlider, hopefully in the end looking something like this:

enter image description here

Where the animation is pretty much identical to the default UISwitch, where the knob moves, pulling the whole view over as well, and resting on the other side with the other option visible.

However, using that library (and messing around with the example included), the closest I can get to that is the following:

enter image description here

Here's a download link to my project if it would help.

Here's the code I customized to do that:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [_switch1 setCanvasImage: [UIImage imageNamed: @"canvas-custom"]];
    [_switch1 setMaskImage: [UIImage imageNamed: @"mask-custom"]];

    [_switch1 setKnobImage: [UIImage imageNamed: @"slider-knob-custom"] forState: UIControlStateNormal];
    [_switch1 setKnobImage: [UIImage imageNamed: @"slider-knob-custom"] forState: UIControlStateHighlighted];
    [_switch1 setKnobImage: [UIImage imageNamed: @"slider-knob-custom"] forState: UIControlStateDisabled];

    [_switch1 setContentImage: [UIImage imageNamed: @"slider-background-custom"] forState: UIControlStateNormal];
    [_switch1 setContentImage: [UIImage imageNamed: @"slider-background-custom"] forState: UIControlStateDisabled];

    [self valueChanged: nil];
//    _switch1.on = YES;
}

I don't know what I'm doing wrong. I have a feeling it's just with image sizes (the images are included with that download link), so if you can just suggest to me the sizing I'd need, or whatever I need to do to fix this, I'd really appreciate the help, it's stumping me greatly.

Was it helpful?

Solution

Your problem is inside the .xib. You have defined a BMXSwitch with a large frame. The frame of the switch must be equal to the size of your button, no more.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top