删除子视图控制器
Remove child view controller
我有以下代码:
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)
}
我想再次调用该函数,但在顶部添加另一个子视图控制器之前删除了子视图控制器。因为第二次有不同的视图控制器,所以应该添加它们或者只是一个不同的数组。
如果我在再次调用该方法时保持原样,则前一个添加的子项显示在滚动边缘的末尾或开始处。
您需要将控制器添加为子控制器,以便稍后将其删除。为此,请使用此代码:
var pagingViewController: PagingViewController?
func removePagingViewController() {
pagingViewController?.removeFromParent()
pagingViewController?.view.removeFromSuperview()
}
这是为了使其模块化。
删除单个child:
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()
}
删除所有children:
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()
}
}
}
我有以下代码:
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)
}
我想再次调用该函数,但在顶部添加另一个子视图控制器之前删除了子视图控制器。因为第二次有不同的视图控制器,所以应该添加它们或者只是一个不同的数组。
如果我在再次调用该方法时保持原样,则前一个添加的子项显示在滚动边缘的末尾或开始处。
您需要将控制器添加为子控制器,以便稍后将其删除。为此,请使用此代码:
var pagingViewController: PagingViewController?
func removePagingViewController() {
pagingViewController?.removeFromParent()
pagingViewController?.view.removeFromSuperview()
}
这是为了使其模块化。
删除单个child:
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()
}
删除所有children:
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()
}
}
}