Вы можете использовать containerView
и управлять представлением как childViewController
.
// The main viewController
class ViewController: UIViewController {
@IBOutlet weak var containerView: UIView!
public lazy var vc1: UITableViewController = {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "TableViewController") as? UITableViewController else { fatalError() }
return vc
}()
public lazy var vc2: UICollectionViewController = {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "CollectionViewController") as? UICollectionViewController else { fatalError() }
return vc
}()
override func viewDidLoad() {
addVC(vc1)
}
// outlet is linked to a UISegmentedControl and trigged when the value is changed
@IBAction func didSelectSegment(_ sender: UISegmentedControl) {
let (vcToAdd, vcToRemove) = sender.selectedSegmentIndex == 0 ? (vc1, vc2) : (vc2, vc1)
removeVC(vcToRemove)
addVC(vcToAdd)
}
func addVC(_ vc: UIViewController) {
addChildViewController(vc)
containerView.addSubview(vc.view)
vc.view.translatesAutoresizingMaskIntoConstraints = false
vc.view.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true
vc.view.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
vc.view.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true
vc.view.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
// additional setup
}
func removeVC(_ vc: UIViewController) {
vc.view.removeFromSuperview()
vc.removeFromParentViewController()
}
}
class TableViewController: UITableViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tvCell") ?? UITableViewCell(style: .default, reuseIdentifier: "tvCell")
cell.textLabel?.text = indexPath.row.description
return cell
}
}
class CollectionViewController: UICollectionViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
collectionView?.reloadData()
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cvCell", for: indexPath) as? CVCell else { fatalError() }
cell.nameLabel.text = indexPath.row.description
return cell
}
}
class CVCell: UICollectionViewCell {
@IBOutlet weak var nameLabel: UILabel!
}
UITableViewController
UICollectionViewController