Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tab bar controller not appearing correctly

I'm trying to connect Tab Bar Controller to existing part of my app, but when I do that it's "malfunctioning".

However when I run Tab Bar Controller part standalone as initial view controller it works properly like in the image below :

enter image description here

This is how app looks when it is run(correct behavior) :

enter image description here

However when I go to this tab bar controller from my main app this is how it looks like this:

enter image description here

My main app looks like this :

Scroll View contains

  • UIView 1
  • UIView 2
  • UIView 3
  • UIView x

Each view does something not related to this tab bar controller. Only one view view x tries to "visit" tab bar controller and display some data there, but it's not. Any ideas?

I have this tab bar controller identifier set to test, and I here is how I do that from my view x :

UITabBarController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"test"];
        [self.view addSubview:newViewController.view];

EDIT :

Entire app :

enter image description here

I'm creating views programatically. That's why I don't have any relationships/segues to the tab bar controller.

SOLUTION :

Change :

UITabBarController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"test"];
            [self.view addSubview:newViewController.view];

To :

UITabBarController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"test"];
        [self addChildViewController:newViewController];
        [self.view addSubview:newViewController.view];
like image 213
London Avatar asked Jun 28 '26 02:06

London


2 Answers

You need to set a root viewController to the navigation Controller

like image 161
Janub Avatar answered Jun 30 '26 16:06

Janub


Just don't do this. From the Apple UITabBarController referenece

Because the UITabBarController class inherits from the UIViewController class, tab bar controllers have their own view that is accessible through the view property. When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.

As I understand it means you must use UITabBarController only as a root view controller of the window. But you can alway use a general UIViewController and add UITabBar there.

(The view offset problem you've met is possible to be fixed, it will not follow the Apple guidelines however and not advised).

like image 27
A-Live Avatar answered Jun 30 '26 17:06

A-Live