简体   繁体   中英

Adding custom view above tab bar controller/navigation controller?

I tried the following code, in an attempt to get the custom view displaying above the tab bar controller (which happens to have a navigation controller within all of it's 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 setting 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's 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?

将视图添加到 tabBarController 视图本身:

[self.tabBarController.view addSubview:yourView];

你可以简单的 addSubview 到窗口:

[self.view.window addSubview:myView];

There isn't really any good way to do this. The various subviews of UINavigationController and UITabBarController are both private, and trying to mess with them is likely to not work correctly. And Apple doesn't give us the tools to create "container" view controllers, so you can't easily embed the UINavigationController/UITabBarController inside another view controller or recreate UINavigationController/UITabBarController yourself.

Your best bet is probably to go ahead and try to create your own "container" view controller, and deal with some things not working right. In particular, the contained view controller's parentViewController will return nil, and therefore various other things on the contained view controller or its sub-controllers will be broken (eg the interfaceOrientation property will be wrong, presentModalViewController:animated: might not work right). Other things may be broken too.

Or you could wait until some future version of iOS actually has support for us to create container view controllers (if ever), and then support only that version and up.

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

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

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

You can do something like that:

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)
}

For those who are looking to add a view, or in my case an indicator view over the tab bar view, here is the code snipet on GitHub with very easy implementation .

Preview

在此处输入图片说明

Github gist

https://gist.github.com/egzonpllana/cc5538f388d8a530e7c393e7344e57a5

Do you mean above as in vertically above the rest of the contents?

Or above as in sitting on-top of the rest of the content?

There's presentModalViewController:animated: but I doubt that's what you want?

I can't comment so I will write this here for anyone else who comes across this page. The answer by Borut which was down voted was actually the easier and more correct answer.

He simply didn't provide you with the correct directions. You need to add the custom view to the Window for your application which is declared in your AppDelegate (in Mono this would be AppDelegate.Window.Add(myView), not in the current View's Window which is null (eg this.View.Window).

This works fine in Xamarin/Mono and I can't see why it wouldn't be the same for Obj-C in the correct code.

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)
  }
}

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