3

I need to use a subclass of the UITabBar for my project because of the following problem Why page Push animation Tabbar moving up in the iPhone X.

I do not use storyboards. How can this be done programmatically?

-- update --

My CustomTabBarController.swift file now looks like this:

import UIKit

@objc class customTabBarController: UITabBarController {
    override var tabBar: UITabBar {
        return customTabBar
    }
}

And my CustomTabBar.swift file looks like this:

import UIKit

class customTabBar: UITabBar {

    override var frame: CGRect {
        get {
            return super.frame
        }
        set {
            var tmp = newValue
            if let superview = superview, tmp.maxY !=
                superview.frame.height {
                tmp.origin.y = superview.frame.height - tmp.height
            }

            super.frame = tmp
        }
    }
}

But this gives me the following error:

Cannot convert return expression of type 'customTabBar.Type' to return type 'UITabBar'
Christoffer
  • 7,436
  • 4
  • 40
  • 42

3 Answers3

11

You should create an instance of your custom tab bar and override the tabBar property inside your UITabBarController subclass with it.

class CustomTabBarController: UITabBarController {
    let customTabBar = CustomTabBar()

    override var tabBar: UITabBar {
        return customTabBar
    }
}    
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • That gives me the following error: Cannot convert return expression of type 'customTabBar.Type' to return type 'UITabBar'. Any ideas? – Christoffer Nov 29 '17 at 21:23
  • 2
    I Think that override the Tabbar like didn't work, I used but without any result. Anyone can confirm?? – Walid Sassi Jan 25 '20 at 13:05
  • You have to make sure that your custom tab bar class inherits from `UITabBar`, otherwise it won't work. – cutsoy Apr 16 '20 at 20:06
  • 1
    My TabBar items all vanished as soon as I apply the override, even by returning UITabBar() itself – Bruce Dec 11 '20 at 00:10
  • @Bruce Make sure you have the UITabBar instance initialized as a separate variable and return that. – Tamás Sengel Dec 11 '20 at 03:09
  • it doesn't work for example with STTabBar. That tabor works for example when I set it inside storyboard so the problem is with your code. – Gargo Feb 23 '23 at 06:29
3

The only solution that works is given in this answer by using setValue(:forKey:) on the UITabBarController.

VoodooBoot
  • 143
  • 1
  • 2
  • 8
-1

The reason you're getting the 'customTabBar.Type' error is because you're returning the name of your class which, against normal convention, is camel cased. You want to be returning an object - an instance of your class - instead.

import UIKit

@objc class customTabBarController: UITabBarController {
    let myCustomTabBar = customTabBar() // If we're writing conventional swift,
                                        // this should be CustomTabBar()
    
    override var tabBar: UITabBar {
        // Because your class name is customTabBar instead of CustomTabBar,
        // this is returning a TYPE instead of an OBJECT
        // return customTabBar

        // If you wanted to return a new version of a tab bar on every
        // get of this object, you'd use the following code which is similar to yours:
        // return customTabBar()

        // More likely, you want to return the SAME tabBar throughout this object's
        // lifecycle.
        return myCustomTabBar
    }
}
  • it doesn't work for example with STTabBar. That tabor works for example when I set it inside storyboard so the problem is with your code. – Gargo Feb 23 '23 at 06:29