Does anyone know why UITextView.layoutSubviews() is not called when rotating a device to portrait mode?
When rotating to landscape mode, these are called:
- UIViewController.viewWillTransition
- UIViewController.viewDidLayoutSubviews
- UITextView.layoutSubviews
- UILabel.layoutSubviews
But when rotating back to portrait, the UILabel.layoutSubviews() is called, but not the UITextView.layoutSubviews. This is in an empty project with no other code apart from traces in these methods.
layoutSubviewsis usually called whensetNeedsLayout()is invoked already in the previous invocation of run loop.If the layout system does not think it needs to be called, it will not be called.
Ideally you should not override this function. You should just call
setNeedsLayout()after makingsuperviewchanges, and let the layout system call the default implementation of this function. Morever, you should define your subview layout needs inside auto-layout so it is able to get correct values from there.If you want immediate update, you should call
layoutIfNeeded().This is because this is one of those methods that are called arbitrarily by UIKit framework and it may not be ideal for your layout needs.