QLag
QLag

Reputation: 823

TableView content inset in UITabBarController

I have 4 UITableViewController in a UITabBarController. They all use the same UITableViewController class, but on the 3 last controllers, the tableView is not correctly displayed : the top of the tableView is displayed under the navigationBar whereas the first is correctly displayed

The first UITableViewController embedded in a UITabBarViewController (index 0)

The same UITableViewController at index 1, 2 and 3 :

I try to solve the problem by setting programmatically the tableView contentInset in "MCVigilanceSingleColorViewController" class (inherited from UITableViewController), but it is applied to my four controllers (then the tableView of my first UITableViewController has now a wrong frame...).

In the storyboard, my 4 UITableViewController use the same class too, and they are all the same : my 4 ViewControllers are exactly the same. The 2nd, 3th and 4th controllers are a "copy/paste" from the first (I just changed the barItemText). Then they all have the same options checked and unchecked. By the way, checking the option "underTopBard" have no effect on my displayed controllers. I juste don't understand why they are displayed differently.

Cay you help me please ? Thx.

Upvotes: 5

Views: 2594

Answers (3)

Peter Theill
Peter Theill

Reputation: 3220

I had the same issue and to me it sounds like a bug related to automaticallyAdjustsScrollViewInsets in case a UIViewController is contained in a UITabBarController. I solved it by adding the code below to my viewDidLoad:

self.automaticallyAdjustsScrollViewInsets = NO;

CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
self.internalTableView.contentInset =
  self.internalTableView.scrollIndicatorInsets =
    UIEdgeInsetsMake(statusBarHeight + navigationBarHeight, 0, tabBarHeight, 0);

where internalTableView is a UIScrollView such as UITableView.

Upvotes: 1

Mathijs Segers
Mathijs Segers

Reputation: 6238

I was having the same issue, and after finding no real proper reasons why it's causing this I decided to put in a "hack" to fix it for me...

Thats why I added this, It's swift but you can convert it to objective-c easy:

    var navBarHeight:CGFloat = 0
    if let nc = self.navigationController {
        navBarHeight += nc.navigationBar.frame.height
        navBarHeight += UIApplication.sharedApplication().statusBarFrame.size.height
    }

    //iterate over vc's
    if let vc = self.viewControllers {
        //enumerrate allows for the tuple, setting a key
        for (key, c) in enumerate(vc) {
            //first view displays correctly
            if (key > 0) {
                if let controller = c as? BaseListViewController {
                    controller.automaticallyAdjustsScrollViewInsets = false
                    controller.tableView.contentInset = UIEdgeInsets(
                        top: navBarHeight,
                        left: 0,
                        bottom: 0,
                        right: 0
                    )
                }
            }
        }
    }
    super.viewDidLoad()

This is in the Base class of my UITabBar (I extended it obviously). Basicly it checks if there are more than one tab which there should be the second and ++ will have their contentInsets adjusted to have extra at top, simular to the navBar + the statusbar.

I Hope this helps someone.

Upvotes: 0

Caleb L
Caleb L

Reputation: 91

Embedding the UITableViewController in a UINavigationController fixes this issue for me. This is pretty easy from the Storyboard. Just select the UITableViewController, then go to Editor > Embed In > Navigation Controller.

Upvotes: 1

Related Questions