简体   繁体   English

UIWindow addSubview在ios 9中不起作用

[英]UIWindow addSubview is not working in ios 9

following code working fine in ios 8 ,But in ios 9 i am getting white page with no content. 以下代码在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];

please suggest a method to add a custom view on app level . 请提供一种在应用级别添加自定义视图的方法。

thanks in advance . 提前致谢 。

Unchecking "Use Size Classes" option and then coming back and re-checking it again fixes this issue. 取消选中“使用大小类”选项,然后返回并再次重新检查它可以解决此问题。

  • In the project navigator, select a storyboard or xib file. 在项目导航器中,选择storyboard或xib文件。
  • The file's contents open in Interface Builder. 该文件的内容在Interface Builder中打开。
  • Choose View > Utilities > Show File Inspector. 选择“视图”>“实用程序”>“显示文件检查器”。
  • In the Interface Builder Document section, un-tick the "Use Size Classes" checkbox. 在Interface Builder Document部分中,取消勾选“Use Size Classes”复选框。
  • Leave the menu. 离开菜单。 Come back and tick the "Use Size Classes" checkbox again. 返回并再次勾选“使用大小类”复选框。

The solution with the size classes is weird to me, because I do not experience any issues for now by adding a subview into the window on iOS 9. 具有大小类的解决方案对我来说很奇怪,因为我现在没有遇到任何问题,只需在iOS 9的窗口中添加子视图。

Also i want to point that when the rootViewController is set - all existing views in the window's hierarchy are removed. 此外,我想指出,当设置rootViewController时 - 窗口层次结构中的所有现有视图都将被删除。

From UIWindow documentation: 从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. 将视图控制器分配给此属性(以编程方式或使用Interface Builder)将视图控制器的视图安装为窗口的内容视图。 If the window has an existing view hierarchy, the old views are removed before the new ones are installed. 如果窗口具有现有视图层次结构,则在安装新视图之前将删除旧视图。

So you have to make sure that you add your view into the window hierarchy after the rootViewController is set. 因此,您必须确保在设置rootViewController后将视图添加到窗口层次结构中。

For example when the first controller is loaded - you can lazy load that view only once. 例如,当加载第一个控制器时 - 您只能延迟加载该视图一次。

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

I faced the same issue & fixed it by simply quitting/force quitting Xcode & Simulator( Cmd + option + Esc ) & then restarting & Clean ( Cmd + Shift + k ). 我面临同样的问题并通过简单地退出/强制退出Xcode和Simulator( Cmd + 选项 + Esc )然后重新启动和清理( Cmd + Shift + k )来修复它。 Sometimes Xcode misbehaves, I can only wish I had tried this earlier to save a lot of time. 有时Xcode行为不端,我只能希望我早些时候试过这个来节省很多时间。

Try 尝试

view.alpha = 0

in ViewController and viewDidLoad func 在ViewController和viewDidLoad函数中

Hope it helps 希望能帮助到你

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

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