constraintsWithVisualFormat not working properly?

286 Views Asked by At

I have added the constraint like that but not able to get proper view frame.

   [self addSubview:self.scrollView];
[self.scrollView addSubview:self.contectView];

NSDictionary *views = @{@"scrollView" : self.scrollView , @"contectView" : self.contectView };

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:0 views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView(==64)]" options:0 metrics:0 views:views]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contectView]|" options:0 metrics:0 views:views]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contectView]|" options:0 metrics:0 views:views]];
[self layoutIfNeeded];
NSLog(@"%@",self.scrollView);
NSLog(@"%@",self.contectView);

Console O/p

 <UIScrollView: 0x78fd47a0; frame = (0 0; 0 64); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x78faced0>; layer = <CALayer: 0x78facea0>; contentOffset: {0, 0}; contentSize: {0, 0}>

<UIView: 0x78fae7d0; frame = (0 0; 0 0); layer = <CALayer: 0x78fad970>>

I am aspecting contectView frame height also be 64. but it returns 0

1

There are 1 best solutions below

3
On

In a UIScrollView autolayout works in a different way. If you think about it what you wrote in terms of constraints is like asking your content view to fit the content view, but we want the other way around.
Now you have two options: - provide a contentSize to the scroll view - be sure that our content view has an intrinsic content size bigger that the scroll view frame. You can do that by fixing the size of the content view, or by providing to the content view a set of subviews that guarantees an intrinsic content size.

More info here