简体   繁体   English

如何使objectAtIndex:0始终可见?

[英]How to make objectAtIndex:0 always visible?

I'm trying to create UIView, which I need to be visible in whole application. 我正在尝试创建UIView,它需要在整个应用程序中可见。 Only way, how can I do this is, addSubview to UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0] .Im doing it in AppDelegate . 只有这样,我才能做到这一点,将addSubview添加到UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0] 。我正在AppDelegate做到这一点。 So far everthing is fine. 到目前为止,一切都很好。

 window = [UIApplication sharedApplication].keyWindow;
[[[window subviews] objectAtIndex:0] addSubview:myView];

It's a small uiview in left bottom corner. 这是左下角的一个小uiview。 In this view I want to play video while you can do other things in background. 在这种情况下,我想播放视频,同时可以在后台执行其他操作。 But when I push another ViewController or even change rootViewController , it disappears and I need to add it again. 但是当我推另一个ViewController甚至更改rootViewController ,它消失了,我需要再次添加它。

Does rootViewController adds to objectAtIndex:0 or when I change rootViewController , it removes everthing? rootViewController是否添加到objectAtIndex:0或当我更改rootViewController ,它删除了所有内容? I was tried, readd it in everywhere it disappearing, but there most be a better solution! 我尝试过,在消失的任何地方都读过它,但是最有效的解决方法是! In other words, how can I add it without possibility loosing it? 换句话说,如何添加它而不会丢失它?

Did it disappear or the new views are added on top of it? 它消失了还是在其之上添加了新视图?

Usually the current view controller will always show its own view on top of the window. 通常,当前视图控制器将始终在窗口顶部显示其自己的视图。

If you are showing a background view with image, then there is way to keep a background image in all screen by default ... you have to add it to the window layer, 如果您要显示带有图像的背景视图,则默认情况下可以使用一种方法在所有屏幕上保留背景图像...您必须将其添加到窗口层,

self.view.window.layer.contents = (id)[backgroundImage CGImage];

I don't think you CAN create a view that floats on top of every view controller in your app. 我认为您无法创建一个浮动在应用程序中每个视图控制器顶部的视图。 It goes against Apple's design for the way views and view controllers are supposed to function. 视图和视图控制器应该起作用的方式与苹果的设计背道而驰。

You could make your app a single view controller app, with that view controller having a container view the size of the screen. 您可以使您的应用程序成为单个视图控制器应用程序,并且该视图控制器具有容器视图屏幕大小。 Then you could embed your app's "real view controller tree in that container view using an embed segue. Then you could put your floating view on that topmost view controller and get the effect you want. 然后,您可以使用嵌入segue将应用程序的“真实视图控制器树”嵌入到该容器视图中。然后,可以将浮动视图放在该最顶部的视图控制器上,并获得所需的效果。

Create a new UIWindow and give it a high enough windowLevel (ie. UIWindowLevelStatusBar should do the trick). 创建一个新的UIWindow并给它足够高的windowLevel (即UIWindowLevelStatusBar应该可以解决问题)。 Then add the view that you want to be visible to the window and finally make it visible by setting the hidden property to NO . 然后将想要显示的视图添加到窗口中,最后通过将hidden属性设置为NO使其可见。

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

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