UPDATE
我已经发帖了另一个解决方案在这里 https://stackoverflow.com/a/16889377/77567我认为这更简单、更好,并且可以在故事板中使用。
ORIGINAL
使用适当的超级视图、位置和大小在笔尖中创建滚动视图。
接下来,创建一个完全独立的、顶级的 UIView
实例通过拖动UIView
从调色板中取出并将其放入任何现有视图之外的工作区域中。在属性检查器中,将“大小”弹出窗口设置为“无”,并确保状态栏、顶部栏和底部栏均设置为“无”。这是一个例子:
这个新的顶级视图将是您的内容视图。给你的视图控制器两个出口:scrollView
and contentView
:
@interface MyViewController
@property (nonatomic, weak) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIView *contentView;
@end
在笔尖中,连接scrollView
出口到滚动视图并连接contentView
内容视图的出口。
在内容视图内构建内容视图层次结构。将其大小设置为您需要的大小 - 它可以大于 320x480(只要您将其所有条形设置为“无”)。
在你的视图控制器中viewDidLoad
, add 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`.
}