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)
}
}