0

I have a UITabBarController and four UIViewControllers associated with it. I created a custom view in the place of the navigation bar in the UITabBarController subclass, so this custom view will be common for all the UIViewControllers. There are two buttons in the custom view when one of the buttons is tapped, I want to add and open the "FilesViewController.xib" as a child view controller to the currently active UIViewController.

Below is what I tried so far and it is not adding the FilesViewController.xib as a child view controller. What I'm doing wrong?

import UIKit

class RootTabBarController: UITabBarController {


    var topBarHeight:CGFloat = 87.0



    override func viewDidLoad() {
        super.viewDidLoad()
        let containerView = UIView()
        self.view.addSubview(containerView)
        containerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: topBarHeight)
        containerView.backgroundColor = UIColor.init(red: 113.0/255.0, green: 193.0/255.0, blue: 34.0/255.0, alpha: 1.0)

        let filesButton = UIButton()
        containerView.addSubview(filesButton)
        filesButton.frame = CGRect(x: logoutButton.frame.origin.x-46, y: 40, width: 40, height: 40)
        filesButton.setImage(UIImage(named: "folder"), for: .normal)
        filesButton.addTarget(self, action: #selector(openFileViewController(_:)), for: .touchUpInside)

    }


    func configureFilesController() // Old Function
    {

        let filesController = FilesViewController()
        self.addChild(filesController)
        self.view.addSubview(filesController.view)
        filesController.didMove(toParent: selectedViewController)
        let height = view.frame.height
        let width = view.frame.width
        filesController.view.frame = CGRect(x: 0, y: self.view.frame.maxY, width: width, height: height) 

    }

func configureFilesController() // New One
    {
      filesController = FilesViewController.init(nibName: "FilesViewController", bundle: nil)
            self.addChild(filesController)
            self.view.addSubview(filesController.view)
            let height = view.bounds.height
            let width = view.bounds.width
            filesController.view.frame = CGRect(x: 0, y: height/2, width: width, height: height/2)
            filesController.view.layer.cornerRadius = 5.0
            filesController.didMove(toParent: self)

        }


    @objc func openFileViewController(_ sender: UIButton) {
       print("Tapped")
        configureFilesController()
    }

}

import UIKit

class FilesViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .clear
    }

    override func viewWillAppear(_ animated: Bool) {
        prepareBackGroundView()
    }



    override func viewDidAppear(_ animated: Bool) { 
            super.viewDidAppear(animated)
// Adding the below line worked like what I was expecting
            self.view.frame = CGRect(x: 0, y: self.view.bounds.height, width: self.view.bounds.width, height: self.view.frame.height) 
            UIView.animate(withDuration: 0.3) { [weak self] in
                let frame = self?.view.frame
                let yComponent = UIScreen.main.bounds.height - 200
                self?.view.frame = CGRect(x: 0, y: yComponent, width: frame!.width, height: frame!.height)
            }

        }

    func prepareBackGroundView(){
        let blurEffect = UIBlurEffect.init(style: .dark)
        let visualEffect = UIVisualEffectView.init(effect:blurEffect)
        let blurView = UIVisualEffectView.init(effect: blurEffect)

        blurView.contentView.addSubview(visualEffect)
        visualEffect.frame = UIScreen.main.bounds
        blurView.frame = UIScreen.main.bounds

        view.insertSubview(blurView, at: 0)
    }


}
Microcad
  • 23
  • 4

1 Answers1

0

you can find the solution here stackoverflow answer. by using this answer I have created sample program. Here I added two tabs one child controller created in story board and another one created in xib.

my RootTabBarController looks like

import UIKit

class RootTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let child1 = storyBoard.instantiateViewController(identifier: "Child1ViewController")
        let child2 = Child2ViewCOntroller.init(nibName: "Child2ViewCOntroller", bundle: nil)

        let tabItem1:UITabBarItem = UITabBarItem(tabBarSystemItem: .contacts, tag: 0) // customise TabBar with images and title
        let tabItem2:UITabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 1)
        child1.tabBarItem = tabItem1
        child2.tabBarItem = tabItem2

        self.setViewControllers([child1,child2], animated: true)

    }
}
Raju Abe
  • 735
  • 2
  • 10
  • 25
  • FilesViewController's view is not showing up, but it is coming inside the viewDidLoad – Microcad Dec 26 '19 at 10:52
  • find my updated solution, if you face any issues let me know I share bitbucket url – Raju Abe Dec 27 '19 at 11:36
  • Thanks for taking the time out to help me. It was a small mistake from my side which caused FilesViewController not to appear. I have updated the correct function in RootTabBarController and a line of code added in FilesViewController. Your initial tips for .init helped me, however, the above solution adds the viewcontroller programatically to the array and doesn't solve my issue. As said earlier, after adding few lines, I was able to fix the issue myself. Thanks again for your help – Microcad Dec 27 '19 at 12:32