我想在不使用标签栏控制器的情况下制作自定义标签栏

时间:2019-02-07 06:02:58

标签: swift

在此图像中,我在其上放置了一个标签栏,然后在其上放置了标签栏项目。然后,我添加了此代码,但它与内容重叠。标签栏正常工作,并且所有内容都是重叠的。请帮忙。

example

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

            }

在此图像中,在其上放置一个标签栏,然后在其上放置标签栏项目并放置此代码,但是存在一个问题,即它将彼此重叠内容,标签栏正常工作,并且所有内容重叠,请帮忙。

1 个答案:

答案 0 :(得分:0)

似乎您只是将子控制器的视图添加到自定义选项卡-栏控制器视图中。我建议使用添加子视图控制器的机制来获得viewController生命周期的好处。下面是代码片段:-

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

在自定义标签栏viewController中使用这些方法