30

I'm a beginner in iOS development. My question is: is it possible to position UITabBar at the top and how? I can't position my UITabBar at the top of the view.

klcjr89
  • 5,862
  • 10
  • 58
  • 91
AlgroDev
  • 383
  • 1
  • 3
  • 8

9 Answers9

37

Is it possible? Sure, but it violates the human interface guidelines.

Screenshots:

Portrait Landscape Storyboard

Code:

TabController.h:

#import <UIKit/UIKit.h>

@interface TabController : UITabBarController <UITabBarControllerDelegate>

@end

TabController.m:

#import "TabController.h"

@interface TabController ()

@end

@implementation TabController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.delegate = self;
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    [self.tabBar invalidateIntrinsicContentSize];

    CGFloat tabSize = 44.0;

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    if (UIInterfaceOrientationIsLandscape(orientation))
    {
        tabSize = 32.0;
    }

    CGRect tabFrame = self.tabBar.frame;

    tabFrame.size.height = tabSize;

    tabFrame.origin.y = self.view.frame.origin.y;

    self.tabBar.frame = tabFrame;

    // Set the translucent property to NO then back to YES to
    // force the UITabBar to reblur, otherwise part of the
    // new frame will be completely transparent if we rotate
    // from a landscape orientation to a portrait orientation.

    self.tabBar.translucent = NO;
    self.tabBar.translucent = YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
klcjr89
  • 5,862
  • 10
  • 58
  • 91
  • thanks aviatorKen89, i try to create (android fragments) in objective-c that is why i display tab bar in the top – AlgroDev Apr 11 '15 at 16:31
  • is what I can do so via other methods ? – AlgroDev Apr 11 '15 at 16:37
  • I'm not sure what you're asking – klcjr89 Apr 11 '15 at 16:38
  • can i add just buttons in the top ? – AlgroDev Apr 11 '15 at 16:39
  • Your question was about UITabBars, so you will need to ask another question instead. And yes, buttons at the top would work fine. – klcjr89 Apr 11 '15 at 16:40
  • 4
    @AlgroDev I guess you're an Android Dev coming to iOS. Something to keep in mind ios != Android. They are two completely different operating systems. You may have something that works on android but that doesn't mean it will work for iOS. It may in fact completely clash with iOS. In iOS the user expects the tab bar to be along the bottom and navigation bar to be along the top. Careful that you do not put off users by presenting them with something so unexpected. – Fogmeister Apr 11 '15 at 17:08
  • I don't understand how to achieve that, could someone write simple tutorial ? –  Jan 16 '16 at 13:28
  • 1
    Can someone point me to where in HIG it says that tab bars must be at bottom? – LNI Jan 29 '17 at 18:29
  • You should use toolbar for top bars. – GeneCode Mar 01 '17 at 10:05
  • "Is it possible? Sure, but it violates the human interface guidelines." Google do it in https://apps.apple.com/in/app/nybo/id1170384819#?platform=iphone – bibscy Jun 30 '19 at 12:21
9

Swift3: I achieve this by creating a custom class for UITabBarController:

class CustomTabBarController: UITabBarController {

   @IBOutlet weak var financialTabBar: UITabBar!

    override func viewDidLoad() {
        super.viewDidLoad()
        // I've added this line to viewDidLoad
        UIApplication.shared.statusBarFrame.size.height
        financialTabBar.frame = CGRect(x: 0, y:  financialTabBar.frame.size.height, width: financialTabBar.frame.size.width, height: financialTabBar.frame.size.height)
    }

Don't forget to set your Custom Class to TabBarController enter image description here

The result would be like this:

enter image description here

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
8

Here is a working swift 3 example of aviatorken89's code.

  1. First create a new file.
  2. Select source cocoa touch class.
  3. Designate subclass of: UITabBarController
  4. Name the class "CustomTabBarController" or whatever you'd like.
  5. Add the following code to the class.

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        var tabFrame:CGRect = self.tabBar.frame
        tabFrame.origin.y = self.view.frame.origin.y
        self.tabBar.frame = tabFrame
    }
    
  6. If you are using storyboard make sure to change the tab bar class to your custom class via the "Identity inspector".

Leo
  • 336
  • 3
  • 6
7

Swift 5

Add this code to your UITabBarViewController;

  override func viewDidLayoutSubviews() {
    let height = navigationController?.navigationBar.frame.maxY
    tabBar.frame = CGRect(x: 0, y: height ?? 0, width: tabBar.frame.size.width, height: tabBar.frame.size.height)
    super.viewDidLayoutSubviews()
  }

It works for iOS > 13 and iOS < 13

4
override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    tabBar.frame = CGRect(x: 0, y: 0, width: tabBar.frame.size.width, height: tabBar.frame.size.height)
}

storyboardenter image description here

UPDATE IOS 11 ISSUE

the code above doesnt work on ios 11. so here is workaround that i found.

override func viewDidLayoutSubviews() {
    tabBar.frame = CGRect(x: 0, y: 0, width: tabBar.frame.size.width, height: tabBar.frame.size.height)

    super.viewDidLayoutSubviews()
}
axunic
  • 2,256
  • 18
  • 18
  • Can you explain me how to implement this for iPhoneX? @mnemonic23 – PankajSharma Feb 02 '18 at 06:00
  • i think it same with other iphone. - create UITabBarController on storyboard - subclass it. let say "TabViewController" - set tabBar.frame like above code, inside the subclass. – axunic Feb 05 '18 at 04:53
  • How did you make the underline tint green line of the tab button when selected? Thanks. – Ning Jul 27 '18 at 04:11
4

subviews of TabbarController hide back of Tabbar . so for fix use this code :

class CustomTabBarController: UITabBarController ,UITabBarControllerDelegate{

    override func viewDidLayoutSubviews() {
        tabBar.frame = CGRect(x: 0, y: 0, width: tabBar.frame.size.width, height: tabBar.frame.size.height)
        super.viewDidLayoutSubviews()
        delegate = self
        selectedViewController?.view.frame.origin = CGPoint(x: 0, y: tabBar.frame.size.height)
    }

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
          selectedViewController?.view.frame.origin = CGPoint(x: 0, y: tabBar.frame.size.height)
    }

}
Hamed Hosseini
  • 182
  • 2
  • 13
  • 1
    This worked well the first time the screen is shown, however when you go to another screen, pop it off back to this screen, it undoes everything you initially set. How did you solve this problem? – Jay Mar 05 '19 at 20:00
1

You can create a custom tab bar by making it yourself, but apple highly discourages mimicking a system control for a function it was not originally intended to do.

Once again, I discourage you to do so, because it violates the consistency of your app and system apps. But since I'm at it, here we go:

For a custom tab bar, you need to create a view that holds multiple buttons. You also need a container view below the UITabBar (because you want the UITabBar to be at the top). When a button is pressed, you change the UIViewController inside the container.
Its quite simple, but of course, its strongly not recommended.

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
1

After Update to xCode 11.5. Added this code in my controller. work ios 13

override func viewWillLayoutSubviews() {
        renderTabPosition()
        super.viewWillLayoutSubviews()
    }
private func renderTabPosition() {
        tabBar.frame = CGRect(x: 0, y: 0, width: tabBar.frame.size.width, height: tabBar.frame.size.height + 5)        
}
-3

If you want something like an UITabBar on top then how about you create a custom UIView and add any number of UIButtons in it. Then link some ViewControllers with each button through Segue. Done!

You don't need to customise the UITabBar. As aviatorken89 said earlier, it is against the human interface guideline.

Natasha
  • 6,651
  • 3
  • 36
  • 58