0
votes

So I have created a UIScrollView as a subview of the view controller's view in Interface builder and set all auto layout constraints so that it resizes to fill its superview (Trailing, leading, bottom and top constrains set to zero with regards to its superview).

When I run the application, the scrollview does not initially update its frame to be the same size as its superview. the scrollview frame.size.width = 320 even though I am running it on iPhone 6 or iPhone 6 plus simulator.

As soon as I start scrolling, the scrollview frame gets updated to the correct value (same as its superview). why is this happening? shouldn't auto layout automatically resize the scrollview in relation to its superview?

The only way I managed to fix this is to add the following in viewDidLoad:

scrollView.frame = view.frame.

Any ideas why this is happening? It does not seem logical.

Many thanks

1
yes but why do i have to set the frame at all in my code. shouldn't auto layout take care of it automatically since i specified that it needs to have zero leading, trailing, top, bottom space to its superview.plawres

1 Answers

1
votes

Ok, so after some experimentation, I figured it out. I was checking the scrollview frame size in viewDidLoad and it was returning 320 However, it seems that auto layout will only do resizing AFTER viewDidLoad and will call viewDidLayoutSubviews() after it has finished resizing/repositioning...etc

Therefore, when I check the frame size inside viewDidLayoutSubviews, I get the right frame size 375 (for iPhone 6).

Thank you