简体   繁体   English

SubView无法调整设备旋转

[英]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. 因此,我无法设置子视图的UI组件的位置。

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

Any pointer will be helpful. 任何指针都会有所帮助。

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 ) 让您添加的视图成为第一个UIViewController一部分(而不是第二个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). 使用addChildViewController方法将第二个视图控制器添加为第一个视图控制器的子代(这将允许转发这些消息)。

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. 除此之外,如果您的意思是没有调用abcControllershouldautorotate/willanimate ,那是正常现象。

Your main controller should implement some sort of containment logics and forward the shouldautorotate/willanimate to your controller. 您的主控制器应实现某种包含逻辑,并将shouldautorotate/willanimate给您的控制器。 Ie, your main controller could call shouldautorotate/willanimate on your abcController instance, but then it should know it. 即,您的主控制器可以在abcController实例上调用shouldautorotate/willanimate ,但随后它应该知道它。 As an aside, Apple does not suggest doing like this, but this is the only way if you want to support iOS4. 顺便说一句,Apple不建议这样做,但这是要支持iOS4的唯一方法。

Alternatively, you might use UIViewController Containment for iOS>5. 另外,您可以使用iOS> 5的UIViewController Containment

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. 在iOS4上将无法使用。

In order for view controller to receive shouldautorotate/willanimate calls you need it to be in "view hierarchy". 为了使视图控制器能够接收shouldautorotate / willanimate调用,您需要将其置于“视图层次结构”中。 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. 因此,如果仅将其视图添加为子视图,则不会调用这些方法,但如果将此视图控制器推入导航控制器或将其作为模式视图控制器呈现,或使用addChildViewController方法(适用于iOS 5+),则为视图控制器将收到这些消息。 If you just want to add its view as a subview, you can get away with autoresizingMask. 如果仅要将其视图添加为子视图,则可以使用autoresizingMask。 (But then I just don't see a point of creating view controller instead of just custom view) (但是然后我看不到创建视图控制器的意义,而不仅仅是自定义视图)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM