如何在 Interface Builder 中使用 iPhone 主视图之外的 UI 元素设置 UIScrollView?

发布于 2024-09-14 08:45:44 字数 274 浏览 4 评论 0原文

我正在我的 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 技术交流群。

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

发布评论

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

评论(7

[旋木] 2024-09-21 08:45:44

我发现这个问题的最佳解决方法(我认为这对于 Interface Builder 来说是令人尴尬的)是这样的:

  1. 在 UIScrollView 中放置一个“容器”UIView,使用检查器将容器 UIView 的大小设置为所需的大小(例如 320 x 1200)并将您的内容添加到该容器视图中。 (你的按钮、文本字段等)。
  2. 在代码中将 UIScrollView 的 contentSize 设置为与容器 UIView 的大小相同。例如 viewDidLoad 中的某处(例如,scrollView.contentSize = containerView.frame.size;)
  3. 要在 Interface Builder 中修改超出滚动视图边界的内容,您必须每次将容器视图拖到滚动视图之外,进行修改,然后拖动您的容器视图回到 UIScrollView 并构建。

The best workaround I found for this problem (which I consider embarrassing for Interface Builder) is this:

  1. place a "container" UIView inside the UIScrollView, set the size of the container UIView to what is needed (e.g. 320 x 1200) with the inspector and add your content inside that container view. (your buttons, textfields, etc).
  2. set the contentSize for the UIScrollView, in code, to be the same as the size of your container UIView. somewhere in viewDidLoad for example (e.g. scrollView.contentSize = containerView.frame.size;)
  3. To modify content beyond the scrollview's bounds in Interface Builder, you have to drag your container view outside of the scroll view each time, make your modifications, then drag your container view back inside the UIScrollView and build.
一场信仰旅途 2024-09-21 08:45:44

这实际上很简单:

  1. 创建一个新的视图控制器类,例如 MyScrollViewController

  2. 创建一个新的 xib,其中 UIScrollView 作为最顶层视图,并设置 File 的类Owner 为 MyScrollView Controller

  3. 将 File's Owner 的视图属性设置为滚动视图

  4. 拖动滚动视图的底部以创建所需的大小。

  5. 将各种其他 UI 元素定位为滚动视图的子视图

  6. 在 MyScrollViewController.h 中为滚动创建并连接 IBOutlet查看,例如

    @property(保留,非原子)IBOutlet UIScrollView *scrollView;
    
  7. 在MyScrollViewController的viewDidLoad方法中,添加以下代码行:

    self.scrollView.contentSize = self.scrollView.frame.size;
    

Th-th-th-that's all people!

This is actually straightforward:

  1. Create a new view controller class e.g. MyScrollViewController

  2. Create a new xib with a UIScrollView as the topmost view, and set the class of the File's Owner to MyScrollView Controller

  3. Set the view attribute of File's Owner to the scroll view

  4. Drag the bottom of the scrollview to create the desired size.

  5. Position your various other UI elements as sub-views of the scroll view

  6. Create and connect an IBOutlet in MyScrollViewController.h for the scroll view, e.g.

    @property (retain, nonatomic) IBOutlet UIScrollView *scrollView;
    
  7. In MyScrollViewController's viewDidLoad method, add the following line of code:

    self.scrollView.contentSize = self.scrollView.frame.size;
    

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

眼睛会笑 2024-09-21 08:45:44

双击在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.

晨敛清荷 2024-09-21 08:45:44

我也有同样的问题。我最终做的是将另一个 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.

甜味拾荒者 2024-09-21 08:45:44

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

二货你真萌 2024-09-21 08:45:44

最后有一个明智的解决方案来解决所有这些问题,那就是在 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.

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