Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we add more than five tab bar in ios sdk?

I want to add a sixth tab bar item in my project. When I try that I am getting the "More" tab but when I am clicking on the "More" tab nothing happens. How can I fix this?

like image 462
Nishi Avatar asked Dec 02 '22 22:12

Nishi


2 Answers

In swift, subclass UITabBarController and implement:

override var traitCollection: UITraitCollection {
    let realTraits = super.traitCollection
    let lieTrait = UITraitCollection.init(horizontalSizeClass: .regular)
    return UITraitCollection(traitsFrom: [realTraits, lieTrait])
}
like image 153
Shoaib Avatar answered Dec 11 '22 09:12

Shoaib


As of iOS 9 (and possibly 8), there is a simple way to do this. I do not recommend shipping apps using this trick - six tabs is pretty cramped on a 4s or 5 - but it's neat to know.

Notice that iPad tab bars can have more than five tabs just fine without a "more" item.

Apple's new way of determining "tablet or not?" is with size classes.

So, subclass UITabBarController and implement:

-(UITraitCollection *)traitCollection
{
  UITraitCollection
  *realTraits = [super traitCollection],
  *lieTrait = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular];
  return [UITraitCollection traitCollectionWithTraitsFromCollections:@[realTraits, lieTrait]];
}

Add six child view controllers to it, and voila! It looks like Apple practices what they preach, in this case!

(* Note that this will also lie about the horizontal size class to the view controllers in the tabs. You may wish to call - setOverrideTraitCollection:forChildViewController: at an opportune time, possibly in an override of setViewControllers:, to "override" their trait collections back to the real one. That way they won't get confused and try to display inappropriately wide variants of their layout.)

[Edit to add much later: As of iOS 12 or so this trick has an interesting side effect: for 5 or fewer tabs, it turns the tab bar into iPad-style with the titles to the right of the icons.]

like image 43
rgeorge Avatar answered Dec 11 '22 09:12

rgeorge