iOS Autolayout - Frame Size Not Set in Viewdidlayoutsubviews

Autolayout: Incorrect frame sizes in ViewDidAppear

I solved my own question.

I constrain views proportional to VC's height.

The VC that I'm displaying has a Navigation Bar. All the view sizes are correct,(even in viewDidLoad because I call layoutIfNeeded()), until the VC starts drawing the Navigation Bar and shrinks the Superview's height; thus, shrinking its subview's heights.

All I have to do is to Extend edges under the top bar by ticking the appropriate box in the VC's Attributes Inspector. This way VC will not shrink the superview of my subviews.

Since Xcode 8 and iOS10, views are not sized properly on viewDidLayoutSubviews

Now, Interface Builder lets the user change dynamically the size of every view controllers in storyboard, to simulate the size of a certain device.

Before this functionality, the user should set manually each view controller size. So the view controller was saved with a certain size, which was used in initWithCoder to set the initial frame.

Now, it seems that initWithCoder do not use the size defined in storyboard, and define a 1000x1000 px size for the viewcontroller view & all its subviews.

This is not a problem, because views should always use either of these layout solutions:

  • autolayout, and all the constraints will layout correctly your views

  • autoresizingMask, which will layout each view which doesn't have any constraint attached to (note autolayout and margin constraints are now compatible in the same view \o/ !)

But this is a problem for all layout stuff related to the view layer, like cornerRadius, since neither autolayout nor autoresizing mask applies to layer properties.

To answer this problem, the common way is to use viewDidLayoutSubviews if you are in the controller, or layoutSubview if you are in a view. At this point (don't forget to call their super relative methods), you are pretty sure that all layout stuff has been done!

Pretty sure? Hum... not totally, I've remarked, and that's why I asked this question, in some cases the view still has its 1000x1000 size on this method. I think there is no answer to my own question. To give the maximum information about it:

1- it happends only when laying out cells! In UITableViewCell & UICollectionViewCell subclasses, layoutSubview won't be called after subviews would be correctly layed out.

2- As @EugenDimboiu remarked (please upvote his answer if useful for you), calling [myView layoutIfNeeded] on the not-layed out subview will layout it correctly just in time.

- (void)layoutSubviews {
[super layoutSubviews];
NSLog (self.myLabel); // 1000x1000 size
[self.myLabel layoutIfNeeded];
NSLog (self.myLabel); // normal size
}

3- To my opinion, this is definitely a bug. I've submitted it to radar (id 28562874).

PS: I'm not english native, so feel free to edit my post if my grammar should be corrected ;)

PS2: If you have any better solution, feel free not write another answer. I'll move the accepted answer.

Frame calculations in `viewDidLayoutSubviews`

The app gets only one didLayoutSubviews per draw (up to that point, view state changes are just noted with setNeedsLayout). In that sense, didLayoutSubviews is your idea of didCompleteLayoutOfSubviews. After the draw, if another view state change happens, the layout is incomplete again.

In other words, number of didLayout calls doesn't depend on the number of subview adds or frame changes, it depends on the number of draws (not to be confused with the run loop). Before a draw, if the needsLayout flag has been set, layoutSubviews and then didLayoutSubviews will be called exactly once, no matter how much the view hierarchy has been rearranged.

iOS AutoLayout - get frame size width

I think auto layout hasn't had the time to layout your views by the time you call this. Auto layout hasn't happened by the time viewDidLoad is called, because it's called just after the views are loaded and it's only after that that the views are placed into the view controller's view hierarchy and eventually laid out (in the view's layoutSubviews method).

Edit: this answer points out why the scenario in the question doesn't work. @dreamzor's answer points out where to place your code in order to solve it.

When is view frame size set in iOS10 SDK?

In iOS 10, I met this issue right after I started to use Xcode 8 in june, when it was beta. This solution is needed:

Put layoutIfNeeded() right before your layer modifications:

self.view.layoutIfNeeded()

view.layer.cornerRadius = view.frame.size.width / 2
view.clipsToBounds = true

OR

place the code for corner radius to viewDidLayoutSubviews() method:

override func viewDidLayoutSubviews() {

view.layer.cornerRadius = view.frame.size.width / 2
view.clipsToBounds = true

}

When is the frame size of a UIButton finally set

As per my comments above, the frame of a button should be known and can be used when layoutSubviews() is called, this can be overridden in your custom class



Related Topics



Leave a reply



Submit