如何在 iPhone 上缩小 UIPickerView?

发布于 2024-07-22 02:25:13 字数 209 浏览 4 评论 0原文

我想减少 iPhone 应用程序中 UIPickerView 的高度,以便它只显示一行和一列。 选择器视图的高度应等于行的高度。

我正在使用 Interface Builder 构建 UIPickerView,但我找不到调整此控件大小的简单方法。

如何缩小UIPickerView

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.

I'm using Interface Builder to construct the UIPickerView, but I can't find an easy way to re-size this control.

How do you shrink a UIPickerView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痴意少年 2024-07-29 02:25:13

实际上,您可以通过对封闭视图应用仿射变换来稍微缩小整个 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];

通过将选取器放置在包含视图中并向该视图应用缩放变换,将选取器缩放至其原始大小的 75%。 直接将转换应用于 UIPickerView 会导致不良的绘图伪像。

然而,您想要的调整大小的方式最好通过创建自定义控件来实现。

Actually, you can slightly shrink the whole UIPickerView by applying an affine transform to an enclosing view. For example:

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.

However, the kind of resizing you're looking for would be best served by creating a custom control.

属性 2024-07-29 02:25:13

这是可能的而且容易的!

只需以纯文本形式打开 nib 文件,然后找到选取器视图并调整度量:

<object class="IBUIPickerView" id="783900772">
<reference key="NSNextResponder" ref="191373211"/>
<int key="NSvFlags">292</int>
<string key="NSFrame">{{85, 68}, {150, 116}}</string>

仅此而已!

It is possible and easy!

Just open the nib file as plain text, then find the picker view and adjust the measures:

<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!

鲜肉鲜肉永远不皱 2024-07-29 02:25:13

如果您使用故事板和/或自动布局,则 user2248258 的答案实际上有效。 我想按照 Nilambar 的建议添加屏幕截图。

  1. 将 pickerview 放置在另一个容器视图中,其大小符合您的要求 将 pickerview 放入另一个容器视图

  2. 检查容器视图的clipSubviews

  3. 将选取器在该容器视图中水平和垂直居中对齐,也给它尾随位置和前导位置的零约束

这样选取器视图就会被裁剪调整为正确的大小(不会调整大小)。

结果:
输入图片此处描述

The answer of user2248258 actually works if you use storyboards and/or autolayout. There I would like to add screenshots as Nilambar suggested.

  1. place a pickerview in another container view with the size you want it Place a pickerview into another container view

  2. check clipSubviews for the container view

  3. 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).

Result:
enter image description here

谎言月老 2024-07-29 02:25:13

在 Interface Builder 中创建一个简单的 UIView。 设置所需的位置和大小,添加约束。 选中“剪辑子视图”复选框。 然后拖动并 将 Picker View 作为子视图放入此视图中。 添加约束选择器以在容器内水平和垂直对齐。 应该做这项工作。

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.

萌逼全场 2024-07-29 02:25:13

据我所知,如果缩小的话会很乱。

a) UITableView+UIPickerView
我建议您使用“UITableView+UIPickerView中的一行”来执行此操作。您可以像 Windows 中的 dropDownList 一样使用 tableView 中的一行,当您点击该行时将显示 pickerView(一开始是隐藏的) 。


如果 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)
您还可以为选择器添加另一个视图,并在选择某些内容后返回到此视图。

我的结论是:
如果 tableView 中只有几行,请使用 a.
如果 tableView 有很多行,但只有其中一行需要选择器,请使用 b.
如果 tableView 中有很多行并且其中很多需要选择器,请使用 c。

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).

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):


-(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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文