DanielH
DanielH

Reputation: 953

UITabBar overlaps its view

In my app I am using UITabBarController. This is how it looks like in the IB:

enter image description here

TabBarController is used as a rootViewController of the window.

Here is my code where I fill the TabBarController with views:

tabBar = [[UITabBarController alloc]init];

myViewController1 = [[MyViewController1 alloc] initWithNibName:@"View1" bundle:nil];
myViewController2 = [[MyViewController2 alloc] initWithNibName:@"View2" bundle:nil];
myViewController3 = [[MyViewController3 alloc] initWithNibName:@"View3" bundle:nil];

tabBar.viewControllers = [NSArray arrayWithObjects: myViewController1, myViewController2, myViewController3, nil];

[self.view addSubview:tabBar.view];

If some view is shown inside the TabBarController, it is under TabBar (height of this view is red line) - so if I have some TableView, I can't see last one or two rows. How can I achieve to show views inside TabBarController (like green rectangle) - not under tab bar?

I thought that it could be done automatically that if I show something if I am using UITabBarController, it is shown inside not under.

Upvotes: 3

Views: 2355

Answers (2)

Roman
Roman

Reputation: 860

This worked for me

class TabBarController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()
   
    // this will force child controller's view to be above Tab Bar        
    tabBar.isTranslucent = false
}

Upvotes: 1

Artem Abramov
Artem Abramov

Reputation: 4751

Since iOS7 most of the navigational items (including tab bar) are translucent. With that approach the user could see, for example, scrolling cells in UITableView.

There are two ways to deal with your issue:

Opt out extending your controller under translucent UI elements:

if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
    self.edgesForExtendedLayout = UIRectEdgeNone;

But from my point of view the preferable way is too use AutoLayout and

Set up AutoLayouts

You could do that programmatically:

[view setTranslatesAutoresizingMaskIntoConstraints: NO];
id topGuide = myViewController.topLayoutGuide;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (view, topGuide);

[myViewController.view addConstraints:
    [NSLayoutConstraint constraintsWithVisualFormat: @"V:[topGuide]-[view]"
    options: 0
    metrics: nil
    views: viewsDictionary]
];

Or with storyboard: enter image description here

Upvotes: 3

Related Questions