Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positioning UITabBar at the top

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.

like image 298
AlgroDev Avatar asked Apr 11 '15 16:04

AlgroDev


3 Answers

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

Screenshots:

PortraitLandscapeStoryboard

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
like image 104
klcjr89 Avatar answered Oct 21 '22 12:10

klcjr89


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

like image 9
Milad Faridnia Avatar answered Oct 21 '22 12:10

Milad Faridnia


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".

like image 8
Leo Avatar answered Oct 21 '22 12:10

Leo