将 UIView 或 UIViewController 放在所有其他之上 [英] Keep a UIView or UIViewController on top of all others

查看:20
本文介绍了将 UIView 或 UIViewController 放在所有其他之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在继承 UIApplication 以在我的 TouchDisplay 视图中拦截和显示触摸.我想扩展 Application、Window、Delegate 或 Main ViewController,以便将我的 TouchDisplay 视图保持在所有其他视图之上.在我和大多数其他应用程序工作时,视图和控制器一直在添加和删除.我认为正确的答案将能够处理这些添加和删除,并且仍然将 TouchDisplay 视图保持在顶部.

I am subclassing UIApplication to intercept and display touches in my TouchDisplay view. I would like to extend the Application, Window, Delegate or Main ViewController in order to keep my TouchDisplay view on top of all other views. As my and most other applications work, views and controllers are added and removed all the time. I figure the correct answer will be able to deal with these additions and removals and stil keep the TouchDisplay view on top.

感谢您的帮助,乔

推荐答案

以下是您可以采取的几种方法:

Here are a few approaches you could take for this:

  1. 如果您只针对iOS 5+ 和 iPad,您可以制作一个包含两个视图控制器的顶级视图控制器.第一个是TouchDisplay"视图的视图控制器.第二个是应用程序的普通根视图控制器.(即您现有的主视图控制器;您需要在此视图控制器上将 definesPresentationContext 设置为 YES)由于您正在编写容器视图控制器,因此您可以随意订购这两个子视图.有一个 WWDC 2011 讨论视图控制器遏制关于这一点的详细信息.恕我直言,这是最正确"的方法,因为它为您的 TouchDisplay 视图提供了一个视图控制器,处理旋转并且通常与其他人一起玩得很好.(这只适用于 iPad,因为在 iPhone 上,新的模态视图总是覆盖整个屏幕.)

  1. If you're targeting iOS 5+ and iPad only, you can make a top-level view controller which has two contained view controllers. The first would be a view controller for your "TouchDisplay" view. The second would be the application's normal root view controller. (i.e. your existing main view controller; you'll need to set definesPresentationContext to YES on this view controller) Since you're writing the container view controller, you can order those two subviews however you like. There is a WWDC 2011 Talk on view controller containment that goes into great detail about this. This is the most "correct" approach IMHO, because it gives you a view controller for your TouchDisplay view, handles rotation and generally plays nice with others. (This only works on iPad, because on iPhone a new modal view always covers the full screen.)

更直接的方法是使用 addSubview: 将 TouchView 作为子视图添加到现有的顶级 UIWindow 中.大多数应用程序实际上并没有删除顶级视图控制器或添加新的顶级视图控制器;他们只是从中展示其他视图控制器.您在顶级窗口中添加的视图将保留在这些视图之上.当然,您的应用程序可能不遵循此规则,在这种情况下,您可以尝试使用选项 #3.这有旋转问题(当设备旋转时,您的视图不会自动旋转,因此您需要自己执行此操作.)如果遇到问题,您也可以强制您的视图返回顶部,例如 1 秒计时器用其他东西覆盖它.这也不如选项 #1 好,因为您没有获得 UIViewController,只有一个 UIView.

A more straight-forward approach is to simply add your TouchView to your existing top-level UIWindow as a subview with addSubview:. Most applications don't actually remove the top-level view controller or add new top-level ones; they just present other view controllers from it. A view you add in the top-level window will stay above those. Of course, your app may not follow this rule, in which case you might try option #3 instead. This has rotation gotchas (your view will not auto-rotate when the device rotates, so you need to do this yourself.) You could also force your view back to top, say, on a 1-second timer, if you are having issues with other things covering it. This is also not as nice as option #1 because you don't get a UIViewController, just a UIView.

最极端的方法是您可以创建另一个 UIWindow 并为其提供更高的窗口级别,例如 UIWindowLevelAlert 并将您的 TouchDisplay 视图放入其中.然后您可以使窗口背景透明,它会保持在您的正常应用程序内容之上.这里有很多问题,特别是关于自动旋转以及哪个窗口是 keyWindow(这就是为什么如果可以的话,你应该使用 #1 或 #2).

The most extreme approach is that you can create another UIWindow and give it a higher window level, such as UIWindowLevelAlert and put your TouchDisplay view in that. You can then make the window background transparent, and it will stay above your normal app content. There are lots of gotchas here, especially about auto-rotation and which window is the keyWindow (which is why you should use #1 or #2 instead if you can).

这篇关于将 UIView 或 UIViewController 放在所有其他之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆