Reputation: 1
In this image I put a tab bar on it, and then put tab bar items on it. Then I added this code, but it overlaps the content. The tab bar works properly and all of the content is overlapping. Please help.
import UIKit
class TabViewController: UIViewController,UITabBarDelegate {
@IBOutlet weak var tabbar: UITabBar!
var tabViewController:UIViewController?
var tbnViewController:UIViewController?
var membersViewController:UIViewController?
var profileViewController:UIViewController?
override func viewDidLoad() {
super.viewDidLoad()
self.tabbar.delegate = self
}
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
switch item.tag {
case 0:
if tabViewController == nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
tabViewController = storyboard.instantiateViewController(withIdentifier: "TabViewController") as! TabViewController
}
self.view.insertSubview(tabBarController!.view!, belowSubview: self.tabbar)
case 1:
if tbnViewController == nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
tbnViewController = storyboard.instantiateViewController(withIdentifier: "TbnViewController") as! TbnViewController
}
self.view.insertSubview(tbnViewController!.view!, belowSubview: self.tabbar)
case 2:
if membersViewController == nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
membersViewController = storyboard.instantiateViewController(withIdentifier: "MembersViewController") as! MembersViewController
}
self.view.insertSubview(membersViewController!.view!, belowSubview: self.tabbar)
case 3:
if profileViewController == nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
profileViewController = storyboard.instantiateViewController(withIdentifier: "ProfileViewController")
}
self.view.insertSubview(profileViewController!.view!, belowSubview: self.tabbar)
default:
break
}
}
}
In this image a put a tab bar on it and then put tab bar items on it and put this code but there is a problem in it that it will overlap the content to each other the tab bar works properly and all of the content is overlapping please help it.
Upvotes: 0
Views: 515
Reputation: 41
It seems like you are just adding view of sub-controllers to to your custom tab-bar controller view. I would suggest to use the mechanism of adding child view controller to get the benefits of viewController lifecycle. Below is the code snippet:-
extension UIViewController {
func addChildViewController(_ viewController : UIViewController?, forView container: UIView){
guard let viewController = viewController else { return }
viewController.view.translatesAutoresizingMaskIntoConstraints = false
addChildViewController(viewController)
container.addSubview(viewController.view)
let childView = viewController.view
container.addConstraint(NSLayoutConstraint(item: childView!, attribute: .top, relatedBy: .equal, toItem: container, attribute: .top, multiplier: 1.0, constant: 0))
container.addConstraint(NSLayoutConstraint(item: childView!, attribute: .bottom, relatedBy: .equal, toItem: container, attribute: .bottom, multiplier: 1.0, constant: 0))
container.addConstraint(NSLayoutConstraint(item: childView!, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .leading, multiplier: 1.0, constant: 0))
container.addConstraint(NSLayoutConstraint(item: childView!, attribute: .trailing, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: 0))
viewController.didMove(toParentViewController: self)
}
func removeChildVC(_ viewController : UIViewController?){
if let viewController = viewController{
viewController.willMove(toParentViewController: nil)
viewController.view.removeFromSuperview()
}
}
}
Use these methods in your custom tab-bar viewController
Upvotes: 1