【问题标题】:init a UIViewController in portrait and landscape以纵向和横向初始化 UIViewController
【发布时间】:2023-08-26 19:27:02
【问题描述】:

我有一个带有 xib 的 UIViewController,当我分配它时,UIViewController 的视图和宽度不会随着我所处的方向而调整,它总是设置为纵向。即使我在风景。为什么是这样?这是我初始化它的方式:

PNRWebViewController *wvController = [[PNRWebViewController alloc] initWithNibName:@"PNRWebViewController" bundle:nil];

    [self.view addSubview:self.webViewController_.view];

【问题讨论】:

  • 您在 IB 文件中设置的自动调整大小属性是什么? (红色箭头)我怀疑你的问题可能在那里。
  • 如果您在 IB 中选择一个视图并转到 Xcode 右侧面板中的“标尺”选项卡,您将看到它。我也会看看 coneybeare 的答案,那里有很好的信息。

标签: iphone objective-c ios ipad


【解决方案1】:

发生这种情况的原因有很多。查看有关该主题的官方 Apple 问答指南:

Why won't my UIViewController rotate with the device?

  • 视图控制器没有实现这个委托方法: (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;

  • 视图控制器的 UIView 属性嵌入在 UIWindow 中,但旁边还有一个额外的视图控制器。

  • 您已将视图控制器的 UIView 属性作为子视图添加到 UIWindow,但很快就过早地释放了它。

  • 您的 UITabBarController 或 UINavigationController 中的所有子视图控制器不同意共同的方向集。

  • 在不调用 super 的情况下覆盖 -(id)init:-(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle 方法。

【讨论】: