Will AutoLayout not resize images when rotated in a custom container view?

I have a very simple view of a container that contains a sidebar and collapses the view controllers in the content area (think UISplitView, but with a small sidebar / vertical UITabBar).

The container controller also uses autoLayout when resized properly. Content viewController 1 uses autorun and was created with IB, so it has an xib file. Content viewController 2 inherits from UITableViewController and does not use xib.

If I assign viewController 1 as the root view controller and turn around, it will resize, and here are the callbacks I receive in viewController 1:

  • willRotateToInterfaceOrientation
  • updateViewConstraints
  • viewWillLayoutSubviews
  • didRotateFromInterfaceOrientation

However, if I assign the container controller as the root view controller, load viewController 1 and rotate, the size will not work. And I only get the following callbacks inside viewController 1:

  • willRotateToInterfaceOrientation
  • didRotateFromInterfaceOrientation

Inside the view controller container, here is how I change the view controllers:

[self addChildViewController:toViewController]; [toViewController didMoveToParentViewController:self]; // Remove the old view controller [fromViewController willMoveToParentViewController:nil]; [fromViewController.view removeFromSuperview]; [fromViewController removeFromParentViewController]; // Add the new view [self.contentContainerView addSubview:toViewController.view]; 

Now I get the callbacks that should happen, but it looks like neither updateViewConstraints nor viewWillLayoutSubviews are being called. This explains why the resizing does not happen, but why these methods are not called when I put the view controller in the container view?

I also tried explicitly returning YES to my container on

 shouldAutomaticallyForwardAppearanceMethods 

and

 shouldAutomaticallyForwardAppearanceMethods 

although this should already be the default.

In addition, a view controller not executed with IB (view controller 2) resizes correctly when rotated inside the container. However, I do not explicitly use NSLayoutConstraints for this, so I suspect it is the default for Springs and Struts to resize when rotated.

Do I need to forward some other events in the container of the View controller in order to get the right resizing of the view controller when rotating?

+4
source share
2 answers

OK, I think that I have this method missing in the container of the View controller:

 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { self.contentViewController.view.frame = self.contentContainerView.bounds; } 

Although now resizing when it is resized correctly, it still doesn’t cause

 updateViewConstraints 

in the controller of my child view. Interesting

+3
source

It seems like iOS 8 is calling updateViewConstraints for you. But iOS 7 did not. To call this in iOS 7, call setNeedsUpdateConstraints, for example:

 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { [super willAnimateRotationToInterfaceOrientation:interfaceOrientation duration:duration]; BOOL isiOS7 = floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1; if (isiOS7) { // Trigger a call to updateViewConstraints [self.view setNeedsUpdateConstraints]; } } 

In updateLayoutConstraints, a good way to check which orientation is such is to check the orientation of the status bar. This works for 7 and 8.

 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; BOOL layoutAsLandscape = UIInterfaceOrientationIsLandscape(orientation); 
0
source

Source: https://habr.com/ru/post/1443435/


All Articles