简体   繁体   中英

UIScrollView not working properly with layout constraints

Im trying to place 20Textfields vertically and adding constraints to them programmatically and here is my code for that.

//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)]-|"
}

If I use constraintsWithVisualFormat:@"V:|-[containerView(==1000)]-|" , it works fine. But I don't want to assign values statically. How can I set them dynamically? I also have tried constraintsWithVisualFormat:@"V:|[containerView(==scrollView)]|" . But none of them worked for me.They can't be scrolled beyond the height of the device.

Any ideas to fix this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM