4

I have a Tab Bar Controller which, as we know, displays the tab bar at the bottom of the screen. I'm looking for a way to move it to the top. I don't think I can use a simple UITabBar for this as I need to nest UINavigationControllers under it.

Is there any way to move the Tab Bar in a UITabBarController to the top of the screen?

clemens
  • 16,716
  • 11
  • 50
  • 65
emachine
  • 1,135
  • 4
  • 17
  • 25

5 Answers5

9

Try this code in methods "viewDidLayoutSubviews" your UITabBarController

Swift 2.X

  self.tabBar.frame = CGRectMake(0,0,320,50) //example for iPhone 5

Swift 3.X

  self.tabBar.frame = CGRect(0,0,320,50) //example for iPhone 5

Swift 4.X

  self.tabBar.frame = CGRect( x: 0, y: 0, width: 320, height: 50)  //example for iPhone 5
Rashid Latif
  • 2,809
  • 22
  • 26
Ujesh
  • 1,698
  • 2
  • 23
  • 35
4

(in Swift)

In the TabBarViewController.swift file (everyone has named this file as he wants):

  • First: create an IBOutlet of a tab bar and then connect it to the appropiate tab bar in the storyboard or in the nib file.

    @IBOutlet var profileTabBar : UITabBar!
    
  • Second: add this code in the viewDidLoad() function to situate the tab bar where you want (in this case I add de tab bar under the navigation controller). To modify the position change x and y of CGRectMake initializer.

    // [Maybe you don't have a navigation controller] yNavBar indicates the height of the navigation bar. 
    var yNavBar = self.navigationController?.navigationBar.frame.size.height
    // yStatusBar indicates the height of the status bar 
    var yStatusBar = UIApplication.sharedApplication().statusBarFrame.size.height
    // Set the size and the position in the screen of the tab bar
    profileTabBar.frame = CGRectMake(0, yNavBar! + yStatusBar + profileTabBar.frame.size.height, profileTabBar.frame.size.width, profileTabBar.frame.size.height)
    
0

I dont think so. The only thing I can think of is to use UITabBar instead of UITabbarController ...

This might be a better option anyway if you are considering nesting UINavigationControllers for each view loaded for the different tabs.

Kal
  • 24,724
  • 7
  • 65
  • 65
0

in the viewDidLayoutSubviews() put this code like this

override func viewDidLayoutSubviews() {
    tabBar.frame = CGRect(x: 0,y: 0,width: UIScreen.main.bounds.width,height: 30)
}
Chathuranga
  • 316
  • 4
  • 12
-2

I use Tab Bar Controller then I simply change the TabBar position in the Tab Bar Controller in the ViewDidLoad()

import UIKit

class TabbarViewController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.tabBar.frame = CGRect(origin: CGPoint(x: 0,y :64), size: CGSize(width: 400, height: 50))

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

Here is the Screen Short attached of the required result... Tab bar on top in tab bar view conmtroller

Note: It is in swift 3 you can change the syntax to swift 2.* on your own.