0

In my app, I'd like to do something similar to what Spotify does in their app. enter image description here

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?

user4992124
  • 1,574
  • 1
  • 17
  • 35
  • Do you mean the red view in your picture? And what doesn't work? Does it show up in the wrong place, or not at all, or ...? Please be more specific in your question. – koen Apr 02 '18 at 13:35
  • Possible duplicate of [View on Top of UITabBar](https://stackoverflow.com/questions/42384470/view-on-top-of-uitabbar) – koen Apr 02 '18 at 14:03
  • It's really not. This guy wants to show a view on his `UITabBar`, where as I'd like to show a bar that pushes the "application" down. – user4992124 Apr 02 '18 at 15:34
  • Then clarify that in your question. You didn't mention before that you want "a bar that pushes the application down". And please explain what part is not working. This will help get you more useful answers. – koen Apr 02 '18 at 15:36
  • @user4992124, Have you make it work? I want to do exactly the same thing, with tab bar controller, push down 20 points on all possible screens. Thanks – Ning Oct 30 '20 at 04:05

3 Answers3

3

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];
2

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#>];
Mahendra
  • 8,448
  • 3
  • 33
  • 56
1

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

koen
  • 5,383
  • 7
  • 50
  • 89