使用 Interface Builder 创建 UIScrollView 的步骤 [英] steps for creating UIScrollView with Interface Builder

查看:14
本文介绍了使用 Interface Builder 创建 UIScrollView 的步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UIScrollView,但似乎有一些我不明白的基本原理.

假设我想在我的 iphone 应用程序中使用 UIScrollView.我有一个充满 320x700 按钮的视图.显然,这对于 320x480 的 iPhone 来说太大了.所以我知道我必须使用 UIScrollView.但是,这是我应该创建对象的顺序

  1. 创建一个 320x700 的 UIScrollView 作为视图"中的尺寸
  2. 将我所有的按钮等放在这个滚动视图上
  3. 在 viewDidLoad 中将 contentSize 设置为 320x700
  4. 将UIScrollView的delegate设置为File Owner,FileOwner的view设置为UIScrollView
  5. 将视图的大小重置为 320x480.

这样对吗?

这行得通,但对我来说没有意义.我知道 View 应该是画布,我在其中添加了所有 UI 元素.我希望 iPhone 应用程序的画布"为 320x700,并且我希望能够将我的按钮等放在这个 320x700 的画布上.但是如果我不将 UIScrollView 的大小改回 320x480,它就不会滚动,因为我需要将 UIScrollView 的内容大小设置为大于其大小.

但是如果我将 UIScrollView 的大小设置为 320x480,那么我在 Interface Builder 中看不到屏幕和 480 到 700 之间的按钮!所以看起来我应该进行所有编辑并将所有 UI 元素添加到 UIScrollView,然后将其设置回 320x480!

有没有其他更有意义的方法来做到这一点?我在理解这应该如何工作时缺少什么?

解决方案

UPDATE

我已经发布了

I'm in the midst of trying to use a UIScrollView and there appears to be some fundamental thing that I'm just not understanding.

Let's say I want to use a UIScrollView in my iphone app. I have a View filled with buttons that is 320x700. Obviously, this is too big for the iPhone which is 320x480. So I know I have to use a UIScrollView. However, is this the order that I should be creating the objects

  1. Create a UIScrollView that is 320x700 as the dimensions in "View"
  2. Place all my buttons, etc, on this scroll view
  3. In the viewDidLoad set the contentSize to 320x700
  4. Set the delegate of the UIScrollView to the File Owner, and the view of the FileOwner to the UIScrollView
  5. Reset the size of the View back to 320x480.

Is this right?

This works, but it doesn't make sense to me. I get that the View is supposed to be the canvas, where I add all the UI elements. I want the "canvas" of the iPhone app to be 320x700, and I want to be able to put my buttons, etc on this 320x700 canvas. But if I don't change the size of the UIScrollView back to 320x480, it won't scroll, because I need to set the content size of the UIScrollView larger than its size.

But if I set the size of the UIScrollView to 320x480, then I don't see the screen and the buttons between 480 and 700 in Interface Builder! So it seems like I'm supposed to make all my edits and add all my UI elements to the UIScrollView, and then set it back to the 320x480!

Is there some other way to do this that makes more sense? What am I missing in my understanding of how this should work?

解决方案

UPDATE

I have posted another solution here which I think is simpler and better.

ORIGINAL

Here's another way to do this that you might like better:

  1. Set the File's Owner placeholder's custom class to your view controller subclass.
  2. Create the UIScrollView as a top-level object in your nib. Set its size to the screen size (320x460) or just turn on a status bar under "Simulated Metrics".
  3. Connect the scroll view's delegate outlet to File's Owner.
  4. Set the File's Owner's view outlet to the scroll view.
  5. Create a UIView as another top-level object in your nib. This will be your content view.
  6. Set the content view's size to 320x700.
  7. Create a strong (or retain, if not using ARC) outlet named contentView in your view controller (File's Owner) and connect it to the content view.
  8. Put your buttons in the content view.
  9. In your view controller's viewDidLoad, do this:

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self.view addSubview:self.contentView];
        ((UIScrollView *)self.view).contentSize = self.contentView.frame.size;
    }
    

  10. In your view controller's viewDidUnload, do this:

    - (void)viewDidUnload {
        self.contentView = nil;
        [super viewDidUnload];
    }
    


Full size

这篇关于使用 Interface Builder 创建 UIScrollView 的步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆