Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 677
Satyam Avatar asked Dec 24 '13 09:12

Satyam


2 Answers

I got a vertical slider working with iOS 8 and Xcode 6 with only 3 constraints in the storyboard and one line of code. Here's a cropped screencap of the interface:

enter image description here

There are 3 constraints between the vertical slider and the UIImageView next to it:

  1. vSlider.Center Y = Image View.Center Y
  2. vSlider.Width = Image View.Height
  3. vSlider.Center X = Image View.Trailing + 16

And of course the one line of code is:

self.vSlider.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))

It's easy to set up these constraints in the storyboard in Xcode 6, but I think it should be simple to write these constraints in code to support iOS 7 or 6.

like image 149
bugloaf Avatar answered Jan 08 '23 14:01

bugloaf


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.

like image 38
Kolineal Avatar answered Jan 08 '23 12:01

Kolineal