44

I want to use a vertical UISlider. I have no idea about how, so please help me.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
MD.
  • 1,131
  • 4
  • 18
  • 28
  • 3
    This is a duplicate of this question asked yesterday: http://stackoverflow.com/questions/2369926/how-can-i-make-a-vertical-uislider – Brad Larson Mar 04 '10 at 11:59

5 Answers5

105

You have to do this programaticaly. Assuming your UISlider is bound to a variable called slider, add this code in your viewDidLoad method in ViewController.m:

- (void)viewDidLoad {

    [super viewDidLoad];

    CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI * 0.5);
    slider.transform = trans;
}

Let me know if you need any more help on this..

ravinsp
  • 4,150
  • 2
  • 32
  • 43
30

As of Xcode 4.2, you can sort of do the same in Interface Builder.

  • Create a slider
  • Show the "Identity Inspector"
  • Add a "User Defined Runtime Attribute"
  • Set the key path to "layer.transform.rotation.z", the type as "String" ("Number" won't allow floating point values) (possible since Xcode 5) and the value to "-1.570795" (-π/2).

Unfortunately, it will still appear as horizontal in Interface Builder.

But you can position the center and don't need to create a custom class, so it might be useful enough in some cases. The effect is the same as ravinsp's code.

nschum
  • 15,322
  • 5
  • 58
  • 56
7

Swift 3:

slider.transform = slider.transform.rotated(by: CGFloat(0.5 * Float.pi))

// Use 1.5 to invert the shadow of slider if you want

André Abreu
  • 737
  • 7
  • 13
4

In case you work with auto layouts:

In your viewDidLoad, try:

UIView *superView = self.sizeSlider.superview;
[self.sizeSlider removeFromSuperview];
[self.sizeSlider removeConstraints:self.view.constraints];
self.sizeSlider.translatesAutoresizingMaskIntoConstraints = YES;
self.sizeSlider.transform = CGAffineTransformMakeRotation(M_PI_2);
[superView addSubview:self.sizeSlider];

It does not work with constraints, so the trick is to remove the constraints for your uislider. You might have to resize it manually by setting its frame property.

Antzi
  • 12,831
  • 7
  • 48
  • 74
0

Using an IBOutlet and setter observers. If you want the max to be at the bottom, divide pi by positive 2

@IBOutlet weak var slider: UISlider! {
    didSet {
        slider.transform = CGAffineTransform(rotationAngle: .pi / -2)
    }
}
Alex Chase
  • 960
  • 1
  • 7
  • 11