I am trying to create a view in code. Here is the hierarchy of the object of my view
ScrollView must be the same size as the window. The button should be as large as possible. I use the automatic iOS layout, so the restriction lines for all my objects look like
H:|[object]| V:|[object]|
I also set translatesAutoresizingMaskIntoConstraints to NO for each object.
The problem is that the button only gets the default size of the button. Its parent view object (UIView) gets only the size needed to view it.

red: UIScrollView / yellow: UIView
How can I make these views be as large as scrollView?
When I use UIView instead of UIScrollView everything works fine ...
Here is the code:
- (void) viewDidLoad { [super viewDidLoad]; // SCROLL VIEW UIScrollView* scrollView = [UIScrollView new]; scrollView.backgroundColor=[UIColor redColor]; scrollView.translatesAutoresizingMaskIntoConstraints = NO; //CONTAINER VIEW UIView *containerView = [UIView new]; containerView.translatesAutoresizingMaskIntoConstraints = NO; containerView.backgroundColor = [UIColor yellowColor]; [scrollView addSubview:containerView]; // CONSTRAINTS SCROLL VIEW - CONTAINER VIEW [scrollView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containerView]|" options:0 metrics:nil views:@{@"containerView":containerView}]]; [scrollView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView]|" options:0 metrics:nil views:@{@"containerView":containerView}]]; // BUTTON UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.translatesAutoresizingMaskIntoConstraints = NO; [button setTitle:@"I'm way to small" forState:UIControlStateNormal]; [containerView addSubview:button]; // CONSTRAINTS CONTAINER VIEW - BUTTON [containerView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button]|" options:0 metrics:nil views:@{@"button":button}]]; [containerView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button]|" options:0 metrics:nil views:@{@"button":button}]]; self.view = scrollView; }
UPDATE: I really don't know why this is happening. If you configured the view in IB, plug in the outputs and initialize the view in code, scrollview behaves like a normal view (which bends vertically). Its contentSize is not computed correctly. More details here . But how to do it right?
Chrizzor
source share