如何轻松地在笔尖中设计UIScrollView的内容

时间:2012-04-03 05:08:07

标签: xcode cocoa-touch interface-builder

我有一个滚动视图,必须显示大于可用显示区域的视图。 我想轻松设计用户界面,而不必在每次必须进行一些更改时上下移动嵌入式视图。 问题是可见区域外的所有内容在IB中都是不可见的。

是否有任何开关或技巧可以在IB中显示所有内容?

1 个答案:

答案 0 :(得分:27)

更新

我发布了another solution here我认为更简单,更好,并且可以在故事板中使用。

ORIGINAL

使用适当的超视图,位置和大小在笔尖中创建滚动视图。

接下来,通过将UIView拖出调色板并将其放入任何现有视图之外的工作区域,创建一个完全独立的顶级 UIView实例。在“属性”检查器中,将“大小”弹出窗口设置为“无”,并确保“状态栏”,“顶栏”和“底栏”都设置为“无”。这是一个例子:

setting up content view

这个新的顶级视图将是您的内容视图。为视图控制器提供两个出口:scrollViewcontentView

@interface MyViewController

@property (nonatomic, weak) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIView *contentView;

@end

在笔尖中,将scrollView插座连接到滚动视图,然后将contentView插座连接到内容视图。

在内容视图中构建内容视图层次结构。根据需要设置其大小 - 它可以大于320x480(只要您将其所有条形设置为无)。

在视图控制器的viewDidLoad中,添加contentView作为scrollView的子视图,并将scrollView.contentSize设置为contentView的大小:

@implementation MyViewController

@synthesize scrollView = _scrollView;
@synthesize contentView = _contentView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureScrollView];
}

- (void)configureScrollView {
    CGSize size = self.contentView.bounds.size;
    self.contentView.frame = CGRectMake(0, 0, size.width, size.height);
    [self.scrollView addSubview:self.contentView];
    self.scrollView.contentSize = size;

    // If you don't use self.contentView anywhere else, clear it here.
    self.contentView = nil;
    // If you use it elsewhere, clear it in `dealloc` and `viewDidUnload`.
}