简体   繁体   中英

Swift: Moving a tab bar controller to top of view controller

Currently my tab bar controller is at the bottom of the view controller. I was wondering if there is a way to move it to the top of the view controller as I cant seem to find any documentation on it.

Swift 3

let rect = self.tabBar.frame;
self.tabBar.frame  = rect.insetBy(dx: 0, dy:  -view.frame.height + self.tabBar.frame.height + (self.navigationController?.navigationBar.frame.height)!)

Swift 5 | Xcode 11

Try overriding viewWillLayoutSubviews() in your class that extends UITabBarController and then put following:-

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    self.tabBar.invalidateIntrinsicContentSize()

    var tabSize: CGFloat = 44.0;

    var orientation: UIInterfaceOrientation = UIApplication.shared.statusBarOrientation

    if (orientation.isLandscape) {
        tabSize = 32.0;
    }

    var tabFrame: CGRect = self.tabBar.frame;

    tabFrame.size.height = tabSize;

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

    self.tabBar.frame = tabFrame;

    // Set the translucent property to false then back to true 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.isTranslucent = false;
    self.tabBar.isTranslucent = true;
}

Reference: https://stackoverflow.com/a/29580094/6117565

You can try using UIViewController instead of UITabBarController and then place TabBar control on top in your view by constraints. Another way is create your own tabbar. (:

You can't change position on UITabbar . Read Apple documentation for tabbar.

If you want to set effect like tabbar on top of viewController then You can manage that by using one uiview of same size of tabbar and multiple uibuttons in that view which works as tabs. And set that view at top or any position at which you want to show tabbar. You have to manage viewcontrollers displays on button click and manage that view to every view controller. So it is very difficult task, so it is better to use default tabbarController provided by system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM