When a change in size happens for a View Controller (VC) (e.g. on device rotation), we all know this method is called:
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator
The size
parameter is the size of that view controller's view after the size change.
For a view controller (ChildVC) that is the child of a custom container view controller (ContainerVC), then by default the size
parameter given by that method is the ContainerVC's size.
This all works until the ChildVC's view is smaller than the ContainerVC's view. In that case, the ContainerVC needs to implement this method:
-(CGSize)sizeForChildContentContainer:(id)container withParentContainerSize:(CGSize)parentSize
Question: How can I possibly deduct the size of the ChildVC's view inside that method?
The method is called too early, before the size/trait collection has even changed -> calling [self.vc layoutIfNeeded];
to update the constraints does not work (constraints haven't yet been updated to respond to trait change).
The only clue I have at that point is the method's parentSize
parameter which is indeed the future size.
Visual example of what I'm trying to accomplish.
- In Green: ContainerVC
- In Purple: ChildVC
- In Blue: Constraints between
Edit: I am still very much interested in having an answer to this question! But I'm starting to think maybe this problem cannot be dealt with AutoLayout, and needs to be done by hand.
Edit2: Here is the flow of events as described by Apple. I think it shows that we need to know in advance what the size of the ChildViewController will be. This may show that this logic may need to be done by hand...