我的 viewController1
有一个 subview Controller (viewController2
)。 viewController2.view
是 viewController1
View 的 subview 。根据用户的操作,我从其父 View 中删除了 viewController2
的 View 。过了一会儿,我不得不再次将它添加为 subview 。
问题是如果用户在 viewController2
不可见时旋转设备,在将它再次添加到 viewController1 的 View 后,它的框架和它的 subview 被放置,因为设备仍然处于旧方向.甚至 viewController2.view.frame
的高度和 with 也可以互换。
有人有办法解决这个问题吗?提前致谢!
最佳答案
如果没有看到您的代码,我猜您会保留对 View Controller 2 的引用,即使它不可见也是如此。在这种情况下,您需要将 View 旋转事件转发给 Controller ,以便它知道这样的旋转(对您要转发的每个事件执行此操作):
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
// Forward to view controller 2 if it is not displayed
if (!viewController2.view) {
[viewController2 willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
}
更好的设计可能是将 View Controller 2 的 View 设置为隐藏
而不是将其删除,并且它仍然会在没有手动干预的情况下获取事件。
关于ios - 设备方向改变问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14084331/