Search code examples
iosswiftuiviewcontrolleruitabbarcontrolleruitabbar

Present view controller while current view controller has a UITabBar but no UITabBarController


I have a UIViewController with a UITabBar at top of it. I am not using a UITabBarController because I need the UITabBar at the top and not at the bottom of the view. Here is my current view controller :

class CustomTabBarController: UIViewController, UITabBarDelegate
{
    @IBOutlet weak var tabBar: UITabBar!
    var viewControllers: [UIViewController] = []

    //MARK: UIVIewController delegate
    override func viewDidLoad()
    {
        super.viewDidLoad()

        //TabBarController
        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        self.viewControllers.append(storyboard.instantiateViewControllerWithIdentifier("myWorldViewController") as UIViewController)
        self.viewControllers.append(storyboard.instantiateViewControllerWithIdentifier("meViewController") as UIViewController)
        self.viewControllers.append(storyboard.instantiateViewControllerWithIdentifier("welcomeViewController") as UIViewController)
        self.tabBar.selectedItem = self.tabBar.items?.first


        //Graphical Settings
        let itemWidth = self.tabBar.frame.width / 3 as CGFloat
        let image = self.imageWithColor(UIColor.grayColor(), size: CGSizeMake(itemWidth, 49))

        UITabBar.appearance().barTintColor = UIColor.whiteColor()
        UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName:UIFont(name: "Helvetica Neue", size: 20)!], forState: UIControlState.Normal)
        UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -10)
        UITabBar.appearance().tintColor = UIColor.whiteColor()
        UITabBar.appearance().selectionIndicatorImage = image

        //Borders
        self.tabBar.layer.borderColor = UIColor.grayColor().CGColor
        self.tabBar.layer.borderWidth = 1

        self.tabBar(self.tabBar, didSelectItem: (self.tabBar.items?.first)!)
    }

    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem)
    {
        self.view.insertSubview(self.viewControllers[item.tag].view, belowSubview: self.tabBar)
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }

    //MARK: Utils
    func imageWithColor(color: UIColor, size: CGSize) -> UIImage
    {
        let rect = CGRectMake(0, 0, size.width, size.height)
        UIGraphicsBeginImageContext(size)
        let context = UIGraphicsGetCurrentContext()
        CGContextSetFillColorWithColor(context, color.CGColor)
        CGContextFillRect(context, rect )
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image
    }
}

What I need now, it to present a view controller when clicking on a button in one of the tab views. On the destination controller, I need to keep the UITabBar (like when using a normal UITabBarController). How can I do this ?


Solution

  • You want to create a custom Container View Controller. Your CustomTabBarController should be the parent view controller of the view controllers for each tab. It will be the one responsible for displaying a UITabBar and the view of the selected child view controller below.

    Try incorporating the code below into your class:

    let contentView = UIView()
    var selectedViewController: UIViewController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        view.addSubview(contentView)
    
        // ... rest of your code
        // + layout of contentView to take up the area below tabBar
    }
    
    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        if let currentVC = selectedViewController {
            currentVC.willMoveToParentViewController(nil)
            currentVC.view.removeFromSuperview()
            currentVC.removeFromParentViewController()
        }
        let newVC = viewControllers[item.tag]
        addChildViewController(newVC)
        contentView.addSubview(newVC.view)
        newVC.view.frame = contentView.bounds
        newVC.didMoveToParentViewController(self)
        selectedViewController = newVC
    }