如何将UISlider改为Vertical?

时间:2022-01-30 15:22:00

I am customizing an UISlider for my app. I want the slider to be in vertical orientation, but the default UISlider is in horizontal orientation. I couldn't find how to change a UISlider's orientation.

我正在为我的应用定制一个UISlider。我希望滑块是垂直方向的,但是默认的UISlider是水平方向的。我找不到如何改变尤利德的方向。

How can I make a vertical slider in XCode?

如何在XCode中创建一个垂直滑块?

3 个解决方案

#1


49  

By default, a UISlider is horizontal (--). If you wish to make it vertical (|) then you must do this programmatically, probably with a CGAffineTransform. For example, you can add this snippet to viewDidLoad or wherever you deem appropriate:

默认情况下,UISlider是水平的(-)。如果您希望使其垂直(|),那么您必须以编程方式进行,可能需要使用CGAffineTransform。例如,您可以将这个代码片段添加到viewDidLoad或您认为合适的任何地方:

CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI_2);
slider.transform = trans;

#2


2  

In case you are using auto layouts:

如果你正在使用自动布局:

In your viewDidLoad, try:

在你viewDidLoad,试一试:

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.

它不能处理约束,所以技巧是删除uislider的约束。您可能需要通过设置它的frame属性来手动调整它的大小。

#3


1  

I had a problem with :

我有个问题:

CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI_2);
slider.transform = trans;

because I had the code in ViewDidLoad method. Instead, I put the code in ViewDidAppear and it worked fine.

因为我有ViewDidLoad方法中的代码。相反,我将代码放在ViewDidAppear中,它工作得很好。

Edit: it doesn't have to be in the ViewDidAppear, ViewDidLoad works fine too (even better). You could disable auto-resize, or set a constraint for the slider you're rotating so the size doesn't change after rotation.

编辑:它不必出现在ViewDidAppear中,ViewDidLoad也可以正常工作(甚至更好)。您可以禁用自动调整大小,或者为正在旋转的滑块设置约束,以便旋转后大小不会改变。

#1


49  

By default, a UISlider is horizontal (--). If you wish to make it vertical (|) then you must do this programmatically, probably with a CGAffineTransform. For example, you can add this snippet to viewDidLoad or wherever you deem appropriate:

默认情况下,UISlider是水平的(-)。如果您希望使其垂直(|),那么您必须以编程方式进行,可能需要使用CGAffineTransform。例如,您可以将这个代码片段添加到viewDidLoad或您认为合适的任何地方:

CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI_2);
slider.transform = trans;

#2


2  

In case you are using auto layouts:

如果你正在使用自动布局:

In your viewDidLoad, try:

在你viewDidLoad,试一试:

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.

它不能处理约束,所以技巧是删除uislider的约束。您可能需要通过设置它的frame属性来手动调整它的大小。

#3


1  

I had a problem with :

我有个问题:

CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI_2);
slider.transform = trans;

because I had the code in ViewDidLoad method. Instead, I put the code in ViewDidAppear and it worked fine.

因为我有ViewDidLoad方法中的代码。相反,我将代码放在ViewDidAppear中,它工作得很好。

Edit: it doesn't have to be in the ViewDidAppear, ViewDidLoad works fine too (even better). You could disable auto-resize, or set a constraint for the slider you're rotating so the size doesn't change after rotation.

编辑:它不必出现在ViewDidAppear中,ViewDidLoad也可以正常工作(甚至更好)。您可以禁用自动调整大小,或者为正在旋转的滑块设置约束,以便旋转后大小不会改变。