Zvi
Zvi

Reputation: 2424

Moving Tab Bar to the top of the screen swift

I want to have the Tab Bar at the top of the screen. One post suggested to do the followings (I put the following code in the viewDidLoad() of the UITabBarController) :

CODE

let tabBar = self.tabBar

// yStatusBar indicates the height of the status bar
let yStatusBar = UIApplication.sharedApplication().statusBarFrame.size.height

// Set the size and the position in the screen of the tab bar
tabBar.frame = CGRectMake(0, yStatusBar, tabBar.frame.size.width, tabBar.frame.size.height)

There are 2 problems with this solution:

  1. The bottom of the screen is left with a black region where the tab bar was

  2. The Tab bar covers the view at the top of the screen - the constraints of that view is relative to the device but they should be relative to the Tab bar. However when the screen is designed in the IB there is no Tab bar to relate to.

Is there a way to overcome these problems? P.S. I am new to IOS

Upvotes: 1

Views: 4125

Answers (2)

Shami
Shami

Reputation: 11

let tabBar = self.tabBarController?.tabBar
// Set the size and the position in the screen of the tab bar
tabBar?.frame = CGRect(x: 0, y: self.view.frame.height, width: (tabBar?.frame.size.width)!, height:  (tabBar?.frame.size.height)!)

Upvotes: 1

velociraptor11
velociraptor11

Reputation: 604

Although it is against the human interface guidelines there exist a hack if you really want to.

You could create a blank UIView in your storyboard (with proper constraints set up) that would essentially be the placeholder for the tabBar when loaded.

You then set top constraints for your other views relative to this view that you have setup.

This works, but probably not best practice to do so

Upvotes: 0

Related Questions