简体   繁体   中英

Add `UIView` above `UIViewController`

In my app, I'd like to do something similar to what Spotify does in their app. 在此处输入图片说明

They add a UIView (the red bar) above all other views in the app. I tried doing it by adding the following code to my RootSplitViewController , but that doesn't work.

- (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". Any ideas on how to approach this?

You can add your view as a subview inside the UIApplication.shared.keyWindow

For Example in your case:

In 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

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. This has two child viewcontrollers: your current rootVC, and the VC that holds the red view. 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.

See also Apple's documentation here: 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 .

Objective C

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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