ios - 设备方向改变问题

标签 ios uiviewcontroller

我的 viewController1 有一个 subview Controller (viewController2)。 viewController2.viewviewController1 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/

相关文章:

ios - Swift:prepareForSegue 和 instantiateViewController 之间的区别

iphone - iPhone游戏中的简单菜单

javascript - 使用第三方 IME 时,JS keydown keyup keypress 事件在 IOS8 上不起作用

iOS - 获取 Twitter 显示名称和时间

iphone - 适用于 iPhone 的类似 Web 的选项卡

iOS - 嵌套的推送动画可能导致导航栏损坏

ios - 向服务器发送获取数据的请求,发生错误 : The resource could not be loaded

iphone - 无法写入 plist 文件 iOS

ios - 旋转时自动调整 UITableView 的大小,宽度灵活

ios - 检测目标 View Controller viewWillAppear 中的向后/弹出导航