Search code examples
ios7rotationuislidercgaffinetransform

Vertical UISlider in iOS with autolayout


As per my iPad app requirement, i've to show the UISlider vertically.
I'm using iOS7 compiler and deployment target is iOS6.
In the story board I added horizontal UISlider of width 600 pixels. I created IBOutlet in my view controller. I didn't set any auto layout constraints. I'm using the below code to rotate and make it vertical.

self.slider.transform = CGAffineTransformMakeRotation(M_PI_2);

After and before rotation I'm printing the frame size of the slider which is correct. But the slider is not looking proper. Its just showing only knob in the center. How can I rotate the UISlider?


enter image description here


Solution

  • I got it to work this way:

    In viewDidLoad: I added

    [self.slider removeConstraints:self.slider.constraints];
    [self.slider setTranslatesAutoresizingMaskIntoConstraints:YES];
    

    so that it's called before rotating the slider with

    self.slider.transform=CGAffineTransformRotate(self.slider.transform,270.0/180*M_PI);
    

    and there is no need to remove and re-add it to superview.