UICollectionViewController

UICollectionViewController

我正在使用UITableViewController,当单击单元格时,我希望弹出UICollectionViewController。
我已经成功地从UITableViewController演示了UICollectionViewController。但是,我不知道如何把它呈现成只占屏幕一部分的弹出窗口。它现在能感觉到整个景色。
我搜索了stackoverflow,但找不到有效的解决方案。
表格控制器

class SearchViewController: UITableViewController, UISearchBarDelegate {


let cellId = "cellId"
var filteredArray = [String]()
let books = Model().books

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.white
    navigationItem.titleView = navSearchBar
    setupView()
}

// Views.
let navSearchBar = NavSearchBar()
func setupView() {
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    tableView.delegate = self
    tableView.dataSource = self
    navSearchBar.delegate = self
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { // Cancel button is touched.
    self.dismiss(animated: true, completion: nil)
}


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return books.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        book = books[indexPath.row]
    }
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
    cell.textLabel?.text = book
    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    let cellLabelContent = cell!.textLabel!.text // Gets cell name.
    let cellLabelIndex = bibleBooks.firstIndex(of: cellLabelContent!) // searches books array to get correct index of cell name.
    print("Book name:", cellLabelContent!+",", "index:", cellLabelIndex!)
    let notificName = Notification.Name(rawValue: searchedBookIndex)
    NotificationCenter.default.post(name: notificName, object: cellLabelIndex)
    dismiss(animated: true, completion: nil)
    **HERE is where i present the collectionView**
    let layout = UICollectionViewFlowLayout()
    var topVC = UIApplication.shared.keyWindow?.rootViewController
    while((topVC!.presentedViewController) != nil) {
        topVC = topVC!.presentedViewController
        let navController = UINavigationController(rootViewController: ChapterNumbersCollectionView(collectionViewLayout: layout))
        topVC?.present(navController, animated: true, completion: nil)

    }
}

uiCollectionView控制器
class ChapterNumbersCollectionView: UICollectionViewController {

let reuseIdentifier = "CellId"

override func viewDidLoad() {
    super.viewDidLoad()
    setupCollectionView()
    setupNavigationItem()
}

private func setupCollectionView() {
    collectionView.backgroundColor = .yellow
    collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}

private func setupNavigationItem() {
    let button = UIButton(type: .system)
    button.setTitle("cancel", for: [])
    button.addTarget(self, action: #selector(handleDismiss), for: .touchUpInside)
    button.frame = CGRect(x: 0, y: 0, width: 34, height: 34)
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
}

@objc func handleDismiss() {
    dismiss(animated: true, completion: nil)
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 200
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
    cell.backgroundColor = .red
    return cell
}

最佳答案

一种方法是使用view controller containment.didSelectRow中实例化ChapterNumbersCollectionView,但不要显示,而是将其添加为子视图控制器。
然后,将其视图添加为子视图后,使用animateWithDuration.

10-08 19:13