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

查看:144
本文介绍了保持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.

感谢您的帮助,
Joe

Thanks for your help, Joe

推荐答案

以下是您可以采取的一些方法:

Here are a few approaches you could take for this:


  1. 如果您的目标只是 iOS 5+和iPad ,您可以创建一个顶级视图控制器,它有两个包含的视图控制器。第一个是TouchDisplay视图的视图控制器。第二个是应用程序的普通根视图控制器。 (即您现有的主视图控制器;您需要在此视图控制器上将 definesPresentationContext 设置为YES)由于您正在编写容器视图控制器,因此您可以根据需要订购这两个子视图。有一个很好的 WWDC 2011 Talk on view controller containment 这个细节。这是最正确的方法恕我直言,因为它为您的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视图放入其中。然后,您可以使窗口背景透明,并且它将保持在正常应用内容之上。这里有很多问题,特别是关于自动旋转以及哪个窗口是关键窗口(这就是为什么你应该使用#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天全站免登陆