Adding a custom view over the navigation bar controller / navigation controller?

I tried the following code, trying to get the user view displayed above the tab bar controller (which usually has a navigation controller on all its tabs).

The problem is that it overlays on top of the navigation bar, and I want the navigation bar to be moved down.

I tried to set the frame of the tab bar controller, but that didn't move it at all.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Add the tab bar controller current view as a subview of the window //self.tabBarController.view.frame = CGRectMake(0, 62, 320, 320); self.window.rootViewController = self.tabBarController; [self.window makeKeyAndVisible]; // setting up the header view self.headerView = [[HeaderView alloc] initWithFrame:CGRectMake(0, 20, 320, 42)]; [self.window addSubview:self.headerView]; // setting up facebook stuff AgentSingleton *agentSingleton = [AgentSingleton sharedSingleton]; agentSingleton.facebook = [[Facebook alloc] initWithAppId:APP_ID]; return YES; } 

Any ideas?

+12
source share
8 answers

There really is no good way to do this. The various subroutines UINavigationController and UITabBarController are private, and attempts to work with them are likely to work incorrectly. And Apple doesn’t give us the tools to create container view controllers, so you cannot easily embed the UINavigationController / UITabBarController in another view controller or recreate the UINavigationController / UITabBarController yourself.

The best thing is probably to go ahead and try to create your own container controller and handle some things that do not work correctly. In particular, the parentViewController built-in view parentViewController will return zero, so various other elements on the built-in view controller or its subcontrollers will be broken (for example, the interfaceOrientation property will be incorrect, presentModalViewController:animated: may not work correctly). Other things may also be broken.

Or you can wait until some future version of iOS actually supports us for creating container controllers (if at all), and then only supports this version before.

+7
source

Add a view to the tabBarController window:

 [self.tabBarController.view addSubview:yourView]; 
+29
source

You can simply add addSubview to the window:

 [self.view.window addSubview:myView]; 
+8
source

You can do something like this:

 if let indeedTabBarController = self.tabBarController { let buttonHeight: CGFloat = 49 // height of tab bar let buttonWidth = UIScreen.main.bounds.width / 3 // in case if you have 3 tabs let frame = CGRect(x: 0, y: UIScreen.main.bounds.height - buttonHeight, width: buttonWidth, height: buttonHeight) let button = UIButton(frame: frame) button.addTarget(self, action: #selector(self.someAction), for: .touchUpInside) indeedTabBarController.view.addSubview(button) } 
+2
source

You mean above, how upright over the rest of the content?

Or higher, as when sitting on top of the rest of the contents?

There presentModalViewController: animated: but I doubt what you want?

0
source

I can not comment, so I will write it here for everyone who gets on this page. Borut's answer, which was voted, was actually an easier and more correct answer.

He simply did not provide you with the correct directions. You need to add a custom view to the window for your application, which is declared in your AppDelegate (in Mono it will be AppDelegate.Window.Add (myView), and not in the current viewport, which is null (for example, this.View.Window).

This works fine in Xamarin / Mono, and I don't understand why it won't be the same for Obj-C in the correct code.

0
source

I had the same problem and ended up doing something like this:

 self.newView.frame = self.tabBarController.tabBar.frame self.tabBarController.view.addSubview(self.newView) 

Adding the view to the tabBarController and using the frame of the current tab as the position of the new view did the trick.

0
source
 override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) if let indeedTabBarController = self.tabBarController { let buttonHeight: CGFloat = view.safeAreaInsets.bottom + 44 let buttonWidth = UIScreen.main.bounds.width let frame = CGRect(x: 0, y: UIScreen.main.bounds.height - buttonHeight, width: buttonWidth, height: 44) let vw = UIView(frame: frame) vw.backgroundColor = .red indeedTabBarController.view.addSubview(vw) } } 
0
source

All Articles