2013-03-01 2 views
2

UIScrollView를 UIViewController에 스토리 보드에 넣었습니다.내 UIScrollView가 ios6의 자동 레이아웃과 작동하지 않습니다.

- (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에 해당한다 :이 코드를 사용하는 경우?

답변

6

autolayout 환경에서 UIScrollView에 대한 몇 가지 기본 사항이 누락되었다고합니다. 주의 깊게 읽으 ios 6.0 release notes

귀하의 코드가 같아야합니다 :

- (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)]]; 
} 
관련 문제