如何使用autolayout设置UIScrollView的子视图

时间:2015-03-09 14:12:40

标签: ios objective-c uiscrollview

我正在尝试使用十个视图实现Horizo​​ntal scrollview。 它在纵向模式下工作正常,但我也想在景观中使用相同的场景。 我可以通过哪种约束来帮助我吗?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title=@"Demo.....";


    self.myScrollView.pagingEnabled = YES;

    NSInteger numberOfViews = 10;
    for (int i = 0; i < numberOfViews; i++) {

        CGFloat myOrigin = i * self.view.frame.size.width;


        UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];

        myView.backgroundColor = [UIColor purpleColor];


        CGRect myFrame = CGRectMake(10.0f, 70.0f, 200.0f, 25.0f);
        UILabel *myLabel = [[UILabel alloc] initWithFrame:myFrame];
        myLabel.translatesAutoresizingMaskIntoConstraints = NO;
        myLabel.text = [NSString stringWithFormat:@"This is page number %d", i];
        myLabel.font = [UIFont boldSystemFontOfSize:16.0f];
        myLabel.textAlignment =  NSTextAlignmentLeft;
        [myView addSubview:myLabel];


        //set the scroll view delegate to self so that we can listen for changes
        self.myScrollView.delegate = self;
        //add the subview to the scroll view
        [self.myScrollView addSubview:myView];


        NSLayoutConstraint *constraintLeft = [NSLayoutConstraint constraintWithItem:myView
                                                                          attribute:NSLayoutAttributeLeft
                                                                          relatedBy:NSLayoutRelationEqual
                                                                             toItem:self.myScrollView
                                                                          attribute:NSLayoutAttributeLeft
                                                                         multiplier:1.0
                                                                           constant:0.0];
        // 0px to the right of the UIScrollView
        NSLayoutConstraint *constraintRight = [NSLayoutConstraint constraintWithItem:myView
                                                                           attribute:NSLayoutAttributeRight
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:self.myScrollView
                                                                           attribute:NSLayoutAttributeRight
                                                                          multiplier:1.0
                                                                            constant:0.0];
        [self.myScrollView addConstraint:constraintLeft];
        [self.myScrollView addConstraint:constraintRight];

    }

    //set the content size of the scroll view, we keep the height same so it will only
    //scroll horizontally
    self.myScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews,
                                               self.view.frame.size.height);

    //we set the origin to the 3rd page
    CGPoint scrollPoint = CGPointMake(self.view.frame.size.width * 2, 0);
    //change the scroll view offset the the 3rd page so it will start from there
    [myScrollView setContentOffset:scrollPoint animated:YES];

    [self.view addSubview:self.myScrollView];

}

在上面的代码中, 十个视图完全添加到scrollview并在纵向模式下正常工作,但是当我将方向更改为水平时,它将不会调整大小而不会滚动。

1 个答案:

答案 0 :(得分:1)

您需要将subviews width约束等同于scrollView宽度

 for (UIView *subView in subViews)
 {
       NSLayoutConstraint *constraintEqualWidth = [NSLayoutConstraint constraintWithItem:subView
                                                                       attribute:NSLayoutAttributeWidth
                                                                       relatedBy:NSLayoutRelationEqual
                                                                          toItem:self.myScrollView
                                                                       attribute:NSLayoutAttributeWidth
                                                                      multiplier:1.0
                                                                        constant:0.0];
       [subView addConstraint:constraintEqualWidth];
 }