ios 如何将 UISlider 更改为垂直?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8118033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:22:09  来源:igfitidea点击:

How to Change the UISlider to Vertical?

objective-ciosxcodeipaduislider

提问by SeongHo

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

我正在UISlider为我的应用程序定制一个。我希望滑块处于垂直方向,但默认UISlider为水平方向。我找不到如何更改 aUISlider的方向。

How can I make a vertical slider in XCode?

如何在 XCode 中制作垂直滑块?

回答by PengOne

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 viewDidLoador wherever you deem appropriate:

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

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

回答by Antzi

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 属性来手动调整它的大小。

回答by KLD

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 也可以正常工作(甚至更好)。您可以禁用自动调整大小,或为您正在旋转的滑块设置约束,以便旋转后大小不会改变。