我的UIScrollView没有';无法在ios6中使用自动布局
我已将UIViewController中的UIScrollView放入故事板。当我使用此代码时:我的UIScrollView没有';无法在ios6中使用自动布局,ios6,uiscrollview,autolayout,Ios6,Uiscrollview,Autolayout,我已将UIViewController中的UIScrollView放入故事板。当我使用此代码时: - (void)viewDidLoad { [super viewDidLoad]; [_scrollview setContentSize:CGSizeMake(_scrollview.bounds.size.width*2, _scrollview.bounds.size.height)]; [_scrollview setPagingEnabled:YES];
- (void)viewDidLoad
{
[super viewDidLoad];
[_scrollview setContentSize:CGSizeMake(_scrollview.bounds.size.width*2, _scrollview.bounds.size.height)];
[_scrollview setPagingEnabled:YES];
CGRect rect = _scrollview.bounds;
UIView* view = [[UIView alloc]initWithFrame:rect];
[view setBackgroundColor:[UIColor redColor]];
[_scrollview addSubview:view];
rect = CGRectOffset(rect, _scrollview.bounds.size.width, 0);
view = [[UIView alloc]initWithFrame:rect];
view.backgroundColor = [UIColor greenColor];
[_scrollview addSubview:view];
}
如果没有自动布局,它可以正常工作,但是当我启用时,“rect”值等于0。auto layout的等效代码是什么?似乎缺少autolayout环境中有关UIScrollView的一些基本内容。仔细阅读 您的代码应该如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect selfBounds = self.view.bounds;
CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
[_scrollview setPagingEnabled:YES];
UIView* view1 = [[UIView alloc] initWithFrame:selfBounds];
[view1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[view1 setBackgroundColor:[UIColor redColor]];
[_scrollview addSubview:view1];
UIView* view2 = [[UIView alloc]initWithFrame:CGRectOffset(selfBounds, width, 0)];
[view2 setTranslatesAutoresizingMaskIntoConstraints:NO];
view2.backgroundColor = [UIColor greenColor];
[_scrollview addSubview:view2];
[_scrollview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[view1(width)][view2(width)]|" options:0 metrics:@{@"width":@(width)} views:NSDictionaryOfVariableBindings(view1,view2)]];
[_scrollview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view1(height)]|" options:0 metrics:@{@"height":@(height)} views:NSDictionaryOfVariableBindings(view1)]];
[_scrollview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view2(height)]|" options:0 metrics:@{@"height":@(height)} views:NSDictionaryOfVariableBindings(view2)]];
}