[英]UIScrollView not working properly with layout constraints
我試圖垂直放置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)]|"
。 但是它們都不對我有用,它們無法滾動到設備的高度之外。
有任何解決辦法嗎?
嘗試分別為容器視圖添加頂部和高度約束,將高度設置為與scrollView的高度相同,並將高度約束的優先級設置為“低優先級”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.