简体   繁体   English

在UIViewController上方添加UIView

[英]Add `UIView` above `UIViewController`

In my app, I'd like to do something similar to what Spotify does in their app. 在我的应用程序中,我想做一些类似于Spotify在其应用程序中所做的事情。 在此处输入图片说明

They add a UIView (the red bar) above all other views in the app. 他们在应用程序中所有其他视图上方添加了一个UIView (红色条)。 I tried doing it by adding the following code to my RootSplitViewController , but that doesn't work. 我尝试通过将以下代码添加到RootSplitViewController来做到这RootSplitViewController ,但这不起作用。

- (void)viewWillAppear:(BOOL)animated {
    [self.view addSubview:[[BannerView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0f)]];
    for (int i = 0; i < [self.viewControllers count]; i++) {
        UIViewController *vc = [self.viewControllers objectAtIndex:i];
        vc.view.frame = CGRectMake(vc.view.frame.origin.x, 44, vc.view.frame.size.width, vc.view.frame.size.height-44);
    }
}

This just added a red view on top of my UINavigationBar , but not "pushing it down". 这只是在我的UINavigationBar之上添加了一个红色视图,但没有“按下它”。 Any ideas on how to approach this? 关于如何解决这个问题的任何想法?

You can add your view as a subview inside the UIApplication.shared.keyWindow 您可以将视图作为子视图添加到UIApplication.shared.keyWindow

For Example in your case: 例如您的情况:

In Swift 4 在Swift 4中

let window = UIApplication.shared.keyWindow!

let fullScreenView = UIView(frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height))

let yourRedView = UIView(frame: CGRect(x: 0, y: 0, width: fullScreenView.frame.width, height: 100))

fullScreenView.backgroundColor = UIColor.gray        
yourRedView.backgroundColor = UIColor.red

fullScreenView.addSubview(yourRedView)

window.addSubview(fullScreenView)

In Objective-C 在Objective-C中

UIWindow* window = [UIApplication sharedApplication].keyWindow;

UIView* fullScreenView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, window.frame.size.width, window.frame.size.height)];

UIView* yourRedView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, window.frame.size.width, 100)];

[yourRedView setBackgroundColor:[UIColor redColor]];
[fullScreenView setBackgroundColor:[UIColor grayColor]];

[fullScreenView addSubview:yourRedView];
[window addSubview:fullScreenView];

I would make a containerViewController that is at the root of your hierarchy. 我将在您的层次结构的根部创建一个containerViewController。 This has two child viewcontrollers: your current rootVC, and the VC that holds the red view. 它有两个子ViewController:您当前的rootVC和保存红色视图的VC。 Now every time your red view is displayed, your containerVC can change the bounds of the child view controllers and display them both on the screen, even with some animation. 现在,每次显示红色视图时,containerVC都可以更改子视图控制器的边界,并在屏幕上显示它们,甚至带有一些动画。

See also Apple's documentation here: https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html 另请参阅此处的Apple文档: https : //developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html

You can add a view at window level. 您可以在窗口级别添加视图。

let appDelegate = UIApplication.shared.delegate as! AppDelegate
    if let win = appDelegate.window {
        win.addSubview(<#Your View#>)
    }

This will add a view as subview at UIWindow . 这会将视图添加为UIWindow子视图。

Objective C 目标C

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[[appDelegate window] addSubView:<#Your View#>];

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

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