UIWindow addSubview 在 ios 9 中不起作用

UIWindow addSubview is not working in ios 9

以下代码在 ios 8 中运行良好,但在 ios 9 中我得到没有内容的白页。

MRForceUpdateView * mForceUpdateView = [[[NSBundle mainBundle] loadNibNamed:@"MRForceUpdateView" owner:self options:nil] objectAtIndex:0];

mForceUpdateView.frame = self.view.frame;

UIWindow * window = [[[UIApplication sharedApplication] delegate] window];

[window addSubview:mForceUpdateView];

请建议一种在应用级别添加自定义视图的方法。

提前致谢。

尝试使用[window.rootViewController.view addSubview: view];

取消选中 "Use Size Classes" 选项然后返回并再次重新选中可以解决此问题。

  • 在项目导航器中,select 故事板或 xib 文件。
  • 文件内容在 Interface Builder 中打开。
  • 选择“查看”>“实用工具”>“显示文件检查器”。
  • 在 Interface Builder 文档部分,取消勾选 "Use Size Classes" 复选框。
  • 退出菜单。回来打勾 再次选择 "Use Size Classes" 复选框。

大小为 类 的解决方案对我来说很奇怪,因为通过在 iOS 9 上向 window 添加子视图,我目前没有遇到任何问题。

我还想指出,设置 rootViewController 时 - window 层次结构中的所有现有视图都将被删除。

来自 UIWindow 文档:

The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.

因此您必须确保在设置 rootViewController 后将视图添加到 window 层次结构中。

例如,当加载第一个控制器时 - 您可以仅延迟加载该视图一次。

我遇到了同样的问题并通过简单地 quitting/force 退出 Xcode & Simulator(Cmd + option + Esc) & 然后重启 & Clean (Cmd + Shift + k).有时 Xcode 行为不端,我只希望我早点尝试过这个以节省大量时间。

尝试

view.alpha = 0

在 ViewController 和 viewDidLoad 函数中

希望对您有所帮助