UIScrollView 在布局约束下无法正常工作

UIScrollView not working properly with layout constraints

我正在尝试垂直放置 20 个文本字段并以编程方式向它们添加约束,这是我的代码。

//For creating 20 textfields
-(void)createTextFields
{
    //create textfields using for loop
    for (int i=0; i<20; i++) {
        [self createNew:i+1];
    }

}
-(void)createNew:(int )count
{
    UITextField *textField=[[UITextField alloc]init];
    textField.translatesAutoresizingMaskIntoConstraints=NO;
    textField.placeholder=[NSString stringWithFormat:@"Enter text:%u",count];
    textField.font=[textField.font fontWithSize:10];
    textField.backgroundColor = [UIColor colorWithHue:0.8 saturation:0.1 brightness:0.9 alpha:1];
    [containerView addSubview:textField];
    [inputFieldsArr addObject:textField];
    //Left and width
    [containerView addConstraints:[NSLayoutConstraint
                               constraintsWithVisualFormat:@"|-[textField]-|"
                               options:0 metrics:nil
                               views:NSDictionaryOfVariableBindings(textField)]];

    //Top and height
    [containerView addConstraints:[NSLayoutConstraint
                               constraintsWithVisualFormat:@"V:|-count-[textField(30)]"
                               options:0
                                metrics:@{@"count":[NSNumber numberWithInt:count*50]}

                               views:NSDictionaryOfVariableBindings(textField)]];

//    return textField;
}

//Adding a containerview to scrollview
-(void)createScroll
{
    scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    scrollview.showsVerticalScrollIndicator=YES;
    scrollview.scrollEnabled=YES;
    scrollview.userInteractionEnabled=YES;
    scrollview.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:scrollview];
    scrollview.backgroundColor=[UIColor blackColor];
    [scrollview setContentSize:CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height)];
    UIView *view=self.view;
    //Left and width
    [self.view addConstraints:[NSLayoutConstraint
                                constraintsWithVisualFormat:@"|-[scrollview]-|"
                                options:0 metrics:nil
                                views:NSDictionaryOfVariableBindings(scrollview)]];

    //Top and height
    [self.view addConstraints:[NSLayoutConstraint
                                constraintsWithVisualFormat:@"V:|[scrollview]|"
                                options:0
                                metrics:nil

                                views:NSDictionaryOfVariableBindings(scrollview,view)]];



    containerView = [[UIView alloc] init];
    containerView.backgroundColor = [UIColor yellowColor]; // just so I can see it
    containerView.translatesAutoresizingMaskIntoConstraints = NO;
    [scrollview addSubview:containerView];
    [self addCOnstriantsToContainer:0];

    float height=self.view.frame.size.height;
   //    CGSize fittingSize = [scrollview systemLayoutSizeFittingSize: UILayoutFittingCompressedSize];
//    
//    NSLog( @"container fitting size: %@", NSStringFromCGSize( fittingSize ));
    //Left and width

}
//Adding Constriants to containerview
-(void)addCOnstriantsToContainer:(float)height
{
    float width=self.view.frame.size.width;
//    NSLog(@"height:%f",containerView.bounds.size.height);

    [scrollview addConstraints:[NSLayoutConstraint
                                constraintsWithVisualFormat:@"|-[containerView(width)]-|"
                                options:0
                                metrics:@{@"width":[NSNumber numberWithFloat:width]}
                                views:NSDictionaryOfVariableBindings(containerView,scrollview)]];
    //Top and height
    [scrollview addConstraints:[NSLayoutConstraint
                                constraintsWithVisualFormat:@"V:|-[containerView(==1000)]-|"
                                options:0
                                metrics:@{@"height":[NSNumber numberWithFloat:height]}
                                views:NSDictionaryOfVariableBindings(containerView,scrollview)]];

    //constraintsWithVisualFormat:@"V:|-[containerView(1000)]-|"
}

如果我使用 constraintsWithVisualFormat:@"V:|-[containerView(==1000)]-|",它工作正常。但我不想静态赋值。如何动态设置它们?我也试过constraintsWithVisualFormat:@"V:|[containerView(==scrollView)]|"。但是 none 的 me.They 不能滚动到设备的高度之外。

有解决此问题的想法吗?

尝试为container view单独添加top和height约束,设置height与scrollView的height相同,height constraint的优先级设置为Low Priority。