ios 如何更改 UIPickerView 高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/573979/
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
How to change UIPickerView height
提问by Steven Canfield
Is it possible to change the height of UIPickerView? Some applications seem to have shorter PickerViews but setting a smaller frame doesn't seem to work and the frame is locked in Interface Builder.
是否可以更改 UIPickerView 的高度?某些应用程序似乎具有较短的 PickerViews,但设置较小的框架似乎不起作用,并且框架在 Interface Builder 中被锁定。
采纳答案by danielpunkass
It seems obvious that Apple doesn't particularly invite mucking with the default height of the UIPickerView
, but I have found that you can achieve a change in the heightof the view by taking complete control and passing a desired frame size at creation time, e.g:
很明显,Apple 并没有特别邀请使用 的默认高度进行处理UIPickerView
,但我发现您可以通过完全控制并在创建时传递所需的帧大小来改变视图的高度,例如:
smallerPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 120.0)];
smallerPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 120.0)];
You will discover that at various heights and widths, there are visual glitches. Obviously, these glitches would either need to be worked around somehow, or choose another size that doesn't exhibit them.
您会发现在各种高度和宽度下,都会出现视觉故障。显然,这些故障要么需要以某种方式解决,要么选择不显示它们的其他尺寸。
回答by bhavinb
None of the above approaches work in iOS 4.0
以上方法均不适用于 iOS 4.0
The pickerView's height is no longer re-sizable. There is a message which gets dumped to console if you attempt to change the frame of a picker in 4.0:
PickerView 的高度不再可重新调整大小。如果您尝试在 4.0 中更改选择器的框架,则会有一条消息转储到控制台:
-[UIPickerView setFrame:]: invalid height value 66.0 pinned to 162.0
I ended up doing something quite radical to get the effect of a smaller picker which works in both OS 3.xx and OS 4.0. I left the picker to be whatever size the SDK decides it should be and instead made a cut-through transparent window on my background image through which the picker becomes visible. Then simply placed the picker behind (Z Order wise) my background UIImageView so that only a part of the picker is visible which is dictated by the transparent window in my background.
我最终做了一些非常激进的事情,以获得在 OS 3.xx 和 OS 4.0 中都可以使用的较小选择器的效果。我将选择器保留为 SDK 决定的任何大小,而是在我的背景图像上制作了一个透明的透明窗口,通过该窗口可以看到选择器。然后简单地将选择器放在(Z 顺序明智)我的背景 UIImageView 后面,这样只有一部分选择器是可见的,这是由我的背景中的透明窗口决定的。
回答by JRSee
There are only three valid heights for UIPickerView (162.0, 180.0 and 216.0)
.
的有效高度只有三个UIPickerView (162.0, 180.0 and 216.0)
。
You can use the CGAffineTransformMakeTranslation
and CGAffineTransformMakeScale
functions to properly fit the picker to your convenience.
您可以使用CGAffineTransformMakeTranslation
和CGAffineTransformMakeScale
函数来根据您的方便适当地安装选择器。
Example:
例子:
CGAffineTransform t0 = CGAffineTransformMakeTranslation (0, pickerview.bounds.size.height/2);
CGAffineTransform s0 = CGAffineTransformMakeScale (1.0, 0.5);
CGAffineTransform t1 = CGAffineTransformMakeTranslation (0, -pickerview.bounds.size.height/2);
pickerview.transform = CGAffineTransformConcat (t0, CGAffineTransformConcat(s0, t1));
The above code change the height of picker view to half and re-position it to the exact (Left-x1, Top-y1)position.
上面的代码将选择器视图的高度更改为一半,并将其重新定位到准确的(Left-x1, Top-y1)位置。
回答by vijay adhikari
Try:
尝试:
pickerview.transform = CGAffineTransformMakeScale(.5, 0.5);
回答by mmorris
In iOS 4.2 & 4.3 the following works:
在 iOS 4.2 和 4.3 中,以下工作:
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.frame = CGRectMake(0, 0, 320, 180);
[self addSubview:datePicker];
The following does not work:
以下不起作用:
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
[self addSubview:datePicker];
I have an app that is in the app store with a 3 line date picker. I thought the height change may have been prevented because you see the text under the date picker's border, but this happens to the normal 216 height date picker too.
我在应用商店中有一个带有 3 行日期选择器的应用。我认为高度更改可能已被阻止,因为您看到日期选择器边框下的文本,但这也发生在正常的 216 高度日期选择器上。
Which is the bug? Your guess is as good as mine.
哪个是错误?你的猜测和我的一样好。
Also there are 3 valid heights for UIDatePicker
(and UIPickerView
) 162.0, 180.0, and 216.0. If you set a UIPickerView
height to anything else you will see the following in the console when debugging on an iOS device.
此外,UIDatePicker
(和UIPickerView
)162.0、180.0 和 216.0有 3 个有效高度。如果您将UIPickerView
高度设置为其他任何值,您将在 iOS 设备上调试时在控制台中看到以下内容。
2011-09-14 10:06:56.180 DebugHarness[1717:707] -[UIPickerView setFrame:]: invalid height value 300.0 pinned to 216.0
回答by rounak
As of iOS 9, you can freely change UIPickerView
's width and height. No need to use the above mentioned transform hacks.
从 iOS 9 开始,您可以自由更改UIPickerView
的宽度和高度。无需使用上述转换技巧。
回答by rounak
I have found that you canedit the size of the UIPickerView - just not with interface builder. open the .xib file with a text editor and set the size of the picker view to whatever you want. Interface builder does not reset the size and it seems to work. I'm sure apple locked the size for a reason so you'll have to experiment with different sizes to see what works.
我发现您可以编辑 UIPickerView 的大小 - 只是不能使用界面构建器。使用文本编辑器打开 .xib 文件并将选择器视图的大小设置为您想要的任何大小。界面生成器不会重置大小,它似乎可以工作。我确信苹果锁定尺寸是有原因的,所以你必须尝试不同的尺寸才能看到什么有效。
回答by HeikoG
Advantages:
好处:
- Makes setFrame of
UIPickerView
behave like it should - No transform code within your
UIViewController
- Works within
viewWillLayoutSubviews
to rescale/position theUIPickerView
- Works on the iPad without
UIPopover
- The superclass always receives a valid height
- Works with iOS 5
- 使 setFrame
UIPickerView
表现得像它应该的那样 - 您的文件中没有转换代码
UIViewController
- 在内部工作
viewWillLayoutSubviews
以重新缩放/定位UIPickerView
- 无需在 iPad 上运行
UIPopover
- 超类总是收到一个有效的高度
- 适用于 iOS 5
Disadvantages:
缺点:
- Requires you to subclass
UIPickerView
- Requires the use of
pickerView viewForRow
to undo the transformation for the subViews - UIAnimations might not work
- 需要你子类化
UIPickerView
- 需要使用
pickerView viewForRow
来撤消子视图的转换 - UIAnimations 可能不起作用
Solution:
解决方案:
Subclass UIPickerView and overwrite the two methods using the following code. It combines subclassing, fixed height and the transformation approach.
子类 UIPickerView 并使用以下代码覆盖这两个方法。它结合了子类化、固定高度和转换方法。
#define FIXED_PICKER_HEIGHT 216.0f
- (void) setFrame:(CGRect)frame
{
CGFloat targetHeight = frame.size.height;
CGFloat scaleFactor = targetHeight / FIXED_PICKER_HEIGHT;
frame.size.height = FIXED_PICKER_HEIGHT;//fake normal conditions for super
self.transform = CGAffineTransformIdentity;//fake normal conditions for super
[super setFrame:frame];
frame.size.height = targetHeight;
CGFloat dX=self.bounds.size.width/2, dY=self.bounds.size.height/2;
self.transform = CGAffineTransformTranslate(CGAffineTransformScale(CGAffineTransformMakeTranslation(-dX, -dY), 1, scaleFactor), dX, dY);
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
//Your code goes here
CGFloat inverseScaleFactor = FIXED_PICKER_HEIGHT/self.frame.size.height;
CGAffineTransform scale = CGAffineTransformMakeScale(1, inverseScaleFactor);
view.transform = scale;
return view;
}
回答by svarrall
An easy way to change the visible height of a picker view is to embed the picker in a UIView, adjust the parent view's height to the height you want to see of the picker, then enable "Clip Subviews" in Interface Builder on the parent UIView or set view.clipsToBounds = true
in code.
更改选择器视图可见高度的一种简单方法是将选择器嵌入到 UIView 中,将父视图的高度调整为您希望看到的选择器高度,然后在父 UIView 上的 Interface Builder 中启用“剪辑子视图”或view.clipsToBounds = true
在代码中设置。
回答by erran
I wasn't able to follow any of the above advice.
我无法遵循上述任何建议。
I watched multiple tutorials and found thisone the most beneficial:
我看了多个教程,发现这个最有用:
I added the following code to set the new height inside the "viewDidLoad" method, which worked in my app.
我添加了以下代码以在“viewDidLoad”方法中设置新高度,该方法在我的应用程序中有效。
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 120.0)];
[self.view addSubview:picker];
picker.delegate = self;
picker.dataSource = self;
Hope this was helpful!
希望这是有帮助的!