首页 文章

在横向打开时,xib中的UIView显示为纵向

提问于
浏览
0

我刚刚在一个使用故事板的项目中添加了一个单独的视图控制器和xib . 我使用通常的代码从另一个视图控制器内部显示它:

OtherVC *othervc = [[OtherVC alloc] initWithNibName:nil bundle:nil];
[self.view addSubView: othervc.view];

在纵向方向上,他可以直接显示并自动旋转 .

但当我在iPad已经处于横向状态时显示它时,视图的大小是纵向的并且位于左侧 .

这是怎么回事?似乎没有办法为视图添加约束 .

谢谢!

1 回答

  • 0

    这不是提出单独的 viewController 的正确方法 .

    问题是您要在实际视图中添加视图 . 但是 this view have a certain frame that will remain the same when you add it on the view .. in both caselandscapeprotrait ) . 所以,要理解, you should check the orientation and then set the frame of the view properly . 但正如我所说, the way that you are using is wrong.

    在您的情况下 you should 使用 segue 来自 storyboard .

    或者,最多,通过代码,但有类似的东西:

    OtherVC *othervc = [self.storyboard instantiateViewControllerWithIdentifier:@"Identifier"];
    [self presentViewController:othervc animated:YES completion:nil];
    

    其中 Identifier 在故事板中的 viewController 中由右侧的检查员面板设置 .

    或者,如果您想要说“ NIB ”,您应该这样做:

    OtherVC *othervc = [[OtherVC alloc] initWithNibName:@"yourNibName" bundle:[NSBundle mainBundle]]; //Or your correct bundle but i guess will be this.
    [self presentViewController:othervc animated:YES completion:nil];
    

    因此,在此之后,如果您确定已在项目中设置了两个方向,那么您的视图将处于横向状态 .

    现在,您将使用 AutoLayout 或普通掩码规则而不使用AutoLayout来管理视图上的元素 .

相关问题