UIScrollView无法正常使用布局约束

时间:2016-03-16 06:25:56

标签: ios objective-c autolayout nslayoutconstraint

我试图垂直放置20Textfields并以编程方式为它们添加约束,这是我的代码。

//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)]|"。但它们都没有为我工作。它们不能滚动超出设备的高度。

有任何解决此问题的想法吗?

1 个答案:

答案 0 :(得分:0)

尝试分别为容器视图添加顶部和高度约束,设置与scrollView的高度相同的高度,并将高度约束的优先级设置为低优先级。