简体   繁体   English

设备方向更改问题

[英]Device orientation change issue

My viewController1 has a child view controller ( viewController2 ). 我的viewController1有一个子视图控制器( viewController2 )。 viewController2.view is a subview of viewController1 's view. viewController2.view是的子视图viewController1的图。 At the user's action, I remove the view of viewController2 from it's superview. 在用户的操作中,我从它的viewController2删除了viewController2的视图。 After a while I have to add it again as subview. 过了一会儿我不得不再次添加为子视图。

The problem is that if the user rotates the device while viewController2 is not visible, after adding it again to viewController1's view, it's frame and it's subviews are placed as the device was still in the old orientation. 问题是如果用户在viewController2不可见的情况下旋转设备,在将它再次添加到viewController1的视图后,它的框架和子视图将被放置为设备仍处于旧方向。 Even the viewController2.view.frame has the height and with interchanged. 甚至viewController2.view.frame也有高度和互换。

Does anyone have a solution to this? 有人有解决方案吗? Thanks in advance! 提前致谢!

Without seeing your code, I would guess that you are keeping a reference to view controller 2 even when it isn't visible. 在没有看到你的代码的情况下,我猜你正在查看控制器2的引用,即使它不可见。 In that case, you need to forward the view rotation events to the controller so that it knows about the rotation like this (Do this for each event that you want to forward): 在这种情况下,您需要将视图旋转事件转发到控制器,以便它像这样知道旋转(对于您要转发的每个事件执行此操作):

- (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];
    }
}

A better design may be to just set view controller 2's view to hidden instead of removing it, and it will still get the events without manual intervention. 更好的设计可能是将视图控制器2的视图设置为hidden而不是删除它,它仍然可以在没有人工干预的情况下获取事件。

Let me guess your problem. 让我猜你的问题。 Do you want to show the different UIView base on orientation , right? 你想在方向上展示不同的UIView基础吗?

It's about update your viewController2 to load the nib base on orientation. 这是关于更新viewController2以在方向上加载nib。 You can make the landscape and portrait nib to support your different UI due to orientation. 您可以使横向和纵向笔尖由于方向而支持您的不同UI。 Try to look from this Answer 试着从这个答案来看

Hope it helps you 希望它能帮到你

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

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