user1173116
user1173116

Reputation: 39

SubView not adjusting on device rotation

I am adding a subview to my view. Everything works fine. My app supports autorotation. When i rotate my device, my parent views shouldautorotate/willanimate gets called but subviews shouldautorotate/willanimate is not getting called. Because of this i am not able to set the position of UI components of subview.

viewController = [[abcController alloc] initWithNibName:@"abcController" bundle:nil];
[self.view addSubview:viewController.view];

Any pointer will be helpful.

Upvotes: 2

Views: 1617

Answers (3)

sergio
sergio

Reputation: 69027

Your view is lacking the proper autoresize setting:

viewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

Apart from this, if you mean that your abcController's shouldautorotate/willanimate are not getting called, well, that is normal behavior.

Your main controller should implement some sort of containment logics and forward the shouldautorotate/willanimate to your controller. I.e., your main controller could call shouldautorotate/willanimate on your abcController instance, but then it should know it. As an aside, Apple does not suggest doing like this, but this is the only way if you want to support iOS4.

Alternatively, you might use UIViewController Containment for iOS>5.

This resorts to using two methods:

@interface UIViewController (UIContainerViewControllerProtectedMethods)

 - (void)addChildViewController:(UIViewController *)childController;
 - (void)removeFromParentViewController;

@end

Here you can find a good tutorial. This will not work on iOS4.

Upvotes: 1

dariaa
dariaa

Reputation: 6385

In order for view controller to receive shouldautorotate/willanimate calls you need it to be in "view hierarchy". Thus, if you just add its view as a subview, these methods will not get called, but if you push this view controller into navigation controller or present it as a modal view controller or use addChildViewController method(for iOS 5+), view controller will receive those messages. If you just want to add its view as a subview, you can get away with autoresizingMask. (But then I just don't see a point of creating view controller instead of just custom view)

Upvotes: 0

DBD
DBD

Reputation: 23233

That is because you cannot functionally add views from one view controller into the view tree of another view controller and expect the second view controller to get messages.

You need to either

  1. Have the view your adding be part of your first UIViewController (not a second UIViewController)
  2. Use the addChildViewController methods to add the second view controller as a child of the first view controller (which will allow these messages to get forwarded).

Upvotes: 3

Related Questions