【问题标题】:Remove child view controller移除子视图控制器
【发布时间】:2021-03-05 06:14:46
【问题描述】:

我有以下代码:

func setUpPageController(viewControllers: [UIViewController]) {
        let pagingViewController = PagingViewController(viewControllers: viewControllers)
        pagingViewController.menuInsets = UIEdgeInsets(top: 1, left: 0, bottom: 1, right: 0)
        pagingViewController.menuItemSize = .selfSizing(estimatedWidth: 100, height: 40)
        pagingViewController.menuBackgroundColor = UIColor.Custom.secondaryColor_white
        pagingViewController.selectedTextColor = UIColor.Custom.secondaryColor_black
        pagingViewController.menuHorizontalAlignment = .center
        pagingViewController.textColor = UIColor.Custom.secondaryColor_black
        
        // Make sure you add the PagingViewController as a child view
        // controller and contrain it to the edges of the view.
        addChild(pagingViewController)
        view.addSubview(pagingViewController.view)
        pagingViewController.view.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            pagingViewController.view.topAnchor.constraint(equalTo: view.topAnchor),
            pagingViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            pagingViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            pagingViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        ])
        
        pagingViewController.didMove(toParent: self)
    }

我想再次调用该函数,但在添加另一个子视图控制器之前删除了子视图控制器。因为第二次有不同的视图控制器会被添加或者只是一个不同的数组。

如果我在再次调用该方法时保持原样,则添加的前一个子项显示在滚动边缘的末尾或开头。

【问题讨论】:

    标签: swift uipageviewcontroller childviewcontroller


    【解决方案1】:

    这是为了使其模块化。

    要删除一个孩子:

    func removeViewController(_ viewController: UIViewController) {
        // this is to notify the the child that it's about to be removed
        viewController.willMove(toParent: nil)
        // this is to remove the child's view from its superview
        viewController.view.removeFromSuperview()
        // this is to remove the child vc from its parent vc
        viewController.removeFromParent()
    }
    

    删除所有子项:

    func removeAllChildren() {
        if self.children.count > 0 {
            let childrenVC: [UIViewController] = self.children
            for chlidVC in childrenVC {
                chlidVC.willMove(toParent: nil)
                chlidVC.view.removeFromSuperview()
                chlidVC.removeFromParent()
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您需要将控制器添加为子项,以便以后将其删除。为此,请使用以下代码:

      var pagingViewController: PagingViewController?
      
      func removePagingViewController() {
          pagingViewController?.removeFromParent()
          pagingViewController?.view.removeFromSuperview()
      }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-22
        • 1970-01-01
        • 2016-10-22
        相关资源
        最近更新 更多