ios 你如何在 iPhone 上缩小 UIPickerView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/905969/
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 do you shrink a UIPickerView on the iPhone?
提问by Rahul Vyas
I would like to reduce the height of a UIPickerView
in my iPhone app, so that it shows only one row and one column. The height of the picker view should be equal to the height of a row.
我想UIPickerView
在我的 iPhone 应用程序中降低 a 的高度,以便它只显示一行和一列。选择器视图的高度应该等于一行的高度。
I'm using Interface Builder to construct the UIPickerView
, but I can't find an easy way to re-size this control.
我正在使用 Interface Builder 来构造UIPickerView
,但我找不到重新调整此控件大小的简单方法。
How do you shrink a UIPickerView
?
你如何缩小一个UIPickerView
?
回答by Brad Larson
Actually, you can slightly shrink the whole UIPickerView by applying an affine transform to an enclosing view. For example:
实际上,您可以通过对封闭视图应用仿射变换来稍微缩小整个 UIPickerView。例如:
CGSize pickerSize = [pickerView sizeThatFits:CGSizeZero];
pickerTransformView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, pickerSize.width, pickerSize.height)];
pickerTransformView.transform = CGAffineTransformMakeScale(0.75f, 0.75f);
[pickerTransformView addSubview:pickerView];
[self.view addSubview:pickerTransformView];
[pickerTransformView release];
will scale a picker to 75% of its original size by placing it within a containing view and applying a scaling transform to that view. Applying a transform directly to the UIPickerView leads to undesirable drawing artifacts.
通过将选择器放置在包含视图中并对该视图应用缩放变换,将选择器缩放到其原始大小的 75%。将变换直接应用于 UIPickerView 会导致不良的绘图伪像。
However, the kind of resizing you're looking for would be best served by creating a custom control.
但是,您正在寻找的那种调整大小最好通过创建自定义控件来实现。
回答by Alejandra
It is possible and easy!
这是可能的和容易的!
Just open the nib file as plain text, then find the picker view and adjust the measures:
只需以纯文本形式打开 nib 文件,然后找到选择器视图并调整度量:
<object class="IBUIPickerView" id="783900772">
<reference key="NSNextResponder" ref="191373211"/>
<int key="NSvFlags">292</int>
<string key="NSFrame">{{85, 68}, {150, 116}}</string>
That's all!
就这样!
回答by user2248258
Create a simple UIView in Interface Builder. Set desired position and size, add constraints. Check "Clip SubViews" checkbox. Then drag & drop Picker View inside this view as a subview. Add constraints for picker to align horizontally and vertically inside container. Should do the job.
在 Interface Builder 中创建一个简单的 UIView。设置所需的位置和大小,添加约束。选中“剪辑子视图”复选框。然后在此视图中拖放 Picker View 作为子视图。为选择器添加约束以在容器内水平和垂直对齐。应该做这项工作。
回答by palme
The answer of user2248258 actually works if you use storyboards and/or autolayout. There I would like to add screenshots as Nilambar suggested.
如果您使用故事板和/或自动布局,user2248258 的答案实际上有效。在那里,我想按照 Nilambar 的建议添加屏幕截图。
place a pickerview in another container view with the size you want it
align the picker centered horizontally and vertically in that container view, also give it zero constraints for the trailing and leading place
This way the picker view gets clipped off to the correct size (won't be resized).
这样,选择器视图会被剪裁到正确的大小(不会调整大小)。
回答by Chilly Zhong
As far as I know, it will be messed up if you shrink it.
据我所知,如果你缩小它,它会变得一团糟。
a) UITableView+UIPickerView
I recommend you use the "a row in UITableView+UIPickerView
to do this. You can use a row in tableView like the dropDownList in Windows, when you tap on this row will show the pickerView (hidden at first).
a)UITableView+UIPickerView
我建议您使用“a row in”UITableView+UIPickerView
来执行此操作。您可以像 Windows 中的 dropDownList 一样在 tableView 中使用一行,当您点击该行时将显示 pickerView(首先隐藏)。
b) If you have a long lists of data in tableView and only one of the items needs to pick data, you should scroll the view using the following method (make sure to calculate the original position of pickerView as it will be moved up/down together):
b) 如果 tableView 中有很长的数据列表,并且只有一项需要选择数据,则应使用以下方法滚动视图(确保计算 pickerView 的原始位置,因为它将向上/向下移动一起):
-(void)setViewMove:(BOOL)moveUP offset:(CGFloat)offset
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = self.view.frame;
if(moveUP)
{
rect.origin.y-=offset;
rect.size.height+=offset;
}
else //move down
{
rect.origin.y+=offset;
rect.size.height-=offset;
}
self.view.frame = rect;
[UIView commitAnimations];
}
c) You can also add another view for the picker and go back to this view when you have selected something.
c) 您还可以为选择器添加另一个视图,并在选择某些内容后返回此视图。
My conclusion is:
If you have only few lines in tableView, use a.
If you have lots of lines in tableView but only one of them needs picker, use b.
If you have lots of lines in tableView and lots of them need picker, use c.
我的结论是:
如果 tableView 中只有几行,请使用 a.
如果 tableView 中有很多行,但只有其中一个需要选择器,请使用 b。
如果 tableView 中有很多行并且其中很多需要选择器,请使用 c。