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.
Here is a working swift 3 example of aviatorken89's code.
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
}
If you are using storyboard make sure to change the tab bar class to your custom class via the "Identity inspector".
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)
}
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)
}
}
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
The result would be like this:
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.
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