Vertical UISlider in iOS with autolayout

2019-03-18 07:50发布

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

10条回答
再贱就再见
2楼-- · 2019-03-18 08:41

Uncheck Auto-Layout on your ViewController, there is no other option under the SDK 7.0 to make it work vertically :(

查看更多
疯言疯语
3楼-- · 2019-03-18 08:44

You can't use storyboard to build up a UISlider. Build up UISlider by coding.

slider = [[UISlider alloc] initWithFrame:CGRectMake(640, 150, 600, 400)];
[slider.layer setAnchorPoint:CGPointMake(0.0f, 0.0f)];
slider.transform = CGAffineTransformMakeRotation(M_PI/2);
[self.view addSubview:slider];
查看更多
Anthone
4楼-- · 2019-03-18 08:46

Try below code to Rotate the UISlider in Vertical Position..

//To rotate the slider in Vertical Position
CGAffineTransform sliderRotation = CGAffineTransformIdentity;
sliderRotation = CGAffineTransformRotate(sliderRotation, -(M_PI / 2));
sliderBrightness.transform=sliderRotation;
查看更多
Animai°情兽
5楼-- · 2019-03-18 08:47

For me a two-step process worked best (incorporating some of the previous solutions)

Autolayout step) I added a vertical view in IB and used autolayout to link it to neighboring views. Then I added a slider in the view and simply hooked it up to the center of the view. Then hooked up the width of the slider to the height of the view. Finally control-dragged the slider outlet to my ViewController code (as slider)

Code step) Then simply added the to my viewWillAppear (swift-code):
let trans = CGAffineTransformMakeRotation(CGFloat(M_PI_2)); slider.transform = trans;

查看更多
登录 后发表回答