如何在 Interface Builder 中使用 iPhone 主视图之外的 UI 元素设置 UIScrollView?
我正在我的 iPhone 应用程序中构建一个数据输入表单,但数据字段的数量超出了屏幕的显示范围。我想我应该将它们放入 UIScrollView 中,以便用户可以滚动表单。在 Interface Builder 中构建它的最佳方法是什么?我知道我可以通过编程方式完成此操作,但如果可以的话,我想在 Interface Builder 中完成此操作。问题是,如果 UILabels、UITextFields 等落在 iPhone 主屏幕之外(UIScrollView 变得有用的屏幕部分),我该如何布局?
I am building a data entry form in my iPhone app, and there are more data fields than will fit on the screen. I figured I should put them into a UIScrollView so that the user can scroll through the form. What's the best way to build this in Interface Builder? I know that I can do it programmatically, but I'd like to do it in Interface Builder if I can. The problem is, how can I lay out UILabels, UITextFields, etc. if they fall outside of the main iPhone screen--in the part of the screen for which the UIScrollView becomes useful?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我发现这个问题的最佳解决方法(我认为这对于 Interface Builder 来说是令人尴尬的)是这样的:
The best workaround I found for this problem (which I consider embarrassing for Interface Builder) is this:
这实际上很简单:
创建一个新的视图控制器类,例如 MyScrollViewController
创建一个新的 xib,其中 UIScrollView 作为最顶层视图,并设置 File 的类Owner 为 MyScrollView Controller
将 File's Owner 的视图属性设置为滚动视图
拖动滚动视图的底部以创建所需的大小。
将各种其他 UI 元素定位为滚动视图的子视图
在 MyScrollViewController.h 中为滚动创建并连接 IBOutlet查看,例如
在MyScrollViewController的viewDidLoad方法中,添加以下代码行:
Th-th-th-that's all people!
This is actually straightforward:
Create a new view controller class e.g. MyScrollViewController
Create a new xib with a UIScrollView as the topmost view, and set the class of the File's Owner to MyScrollView Controller
Set the view attribute of File's Owner to the scroll view
Drag the bottom of the scrollview to create the desired size.
Position your various other UI elements as sub-views of the scroll view
Create and connect an IBOutlet in MyScrollViewController.h for the scroll view, e.g.
In MyScrollViewController's viewDidLoad method, add the following line of code:
Th-th-th-that's all folks!
在“尺寸检查器”上设置“滚动视图尺寸”的“内容插图”:Bottom = YourFrameHeight - ScreenHeight。它将允许您在 UIScrollView 的从上到上、从下到下的范围内滚动
Set up "Content Insets" of "Scroll View Size" on the "Size inspector": Bottom = YourFrameHeight - ScreenHeight. It will allow you to scroll in ranges of top-to-top, bottom-to-bottom of your UIScrollView
双击在IB中打开
UIScrollView
本身,并将大小增加到您需要的大小或更大(您始终可以在运行时缩小)。然后只需添加元素即可。编辑:
不起作用 - 请参阅此处。
Double click to open the
UIScrollView
itself in IB, and increase the size to the size you need or bigger (you can always shrink at runtime). Then just add the elements.EDIT:
Doesn't work - see here instead.
我也有同样的问题。我最终做的是将另一个 UIView 放入其中并将高度设置为我想要的任何高度。然后我将所有 UI 元素放入其中。这允许您上下拖动内部视图。
I've had the same issue as well. What I ended up doing was putting another UIView inside of it and setting the height to whatever I wanted. Then I put all my UI elements inside of it. This allows you to drag the inner view up and down.
在 xCode5 中(没有故事板,使用自动布局)对我有用的是使用上面的 7 步答案,以“Th-th-th-that's all people!”结尾并添加两个步骤。
8) 将一个新的 UIView 拖到界面生成器中。不进入滚动视图。就靠它自己。将所有控件/视图放入其中,并将其设置为您想要的大小。我将此视图连接为 contentView。
@property(弱,非原子)IBOutlet UIView *contentView;
9)然后在 - (void)viewDidLoad
[self.mainScrollView addSubview:self.contentView];
[self.mainScrollView setContentSize:CGSizeMake(self.contentView.frame.size.width,self.contentView.frame.size.height)];
只是说这对我有用。
What worked for me in xCode5 (no storyboard, using autolayout) is using the 7 step answer above the ends with 'Th-th-th-that's all folks!' and adding two steps.
8) Drag a new UIView to interface builder. Not into the Scroll view. Just on its own. Put all your controls/view into that and make it as big as you want. I hooked up this view as contentView.
@property (weak, nonatomic) IBOutlet UIView *contentView;
9) Then in - (void)viewDidLoad
[self.mainScrollView addSubview:self.contentView];
[self.mainScrollView setContentSize:CGSizeMake(self.contentView.frame.size.width,self.contentView.frame.size.height)];
Just saying that made it work for me.
最后有一个明智的解决方案来解决所有这些问题,那就是在 UIScrollView 内部放置一个 Container View。当 Interface Builder 在 Container View 中单独显示 UIViewController 时,您终于可以看到 UIScrollView 内容的样子,而无需交叉手指和倒立。
您仍然需要以编程方式设置内容大小,但至少您现在可以直观地看到它正在发生的情况。
There is finally a sensible solution to all of this and that is to place a Container View inside of the UIScrollView. As Interface Builder displays the UIViewController inside the Container View separately, you can finally see what your UIScrollView content is going to look like without crossing your fingers and standing on your head.
You still need to programmatically set your content size but a least you can now visualise what it going on.