【问题标题】:Making a Popup ViewController out of a UICollectionViewController使用 UICollectionViewController 制作弹出 ViewController
【发布时间】:2019-06-28 16:20:33
【问题描述】:

我正在使用 UITableViewController,当单击单元格时,我希望弹出 UICollectionViewController。

我已经成功地从 UITableViewController 展示了 UICollectionViewController。但是,我无法弄清楚如何将其呈现为仅占据屏幕一部分的弹出窗口。它目前感觉整个视图。

我在 stackoverflow 上进行了搜索,但找不到有效的解决方案。

UITableViewController

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)

    }      
}

UICollectionViewController

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
}

【问题讨论】:

  • 您的问题非常广泛,但一种方法是摆脱 UITableViewController 和 UICollectionViewController 并将 UITableView 和 UICollectionView 放入视图控制器中。 collectionview 的初始高度约束为 0,当您在 tableview 中选择一个项目时,将该高度约束设置为屏幕高度的一半。
  • 我建议在您的 Tableview 上添加一个 UIView 并使用子类添加它并在 UIView 中创建您的 Collectionview
  • @OmerTekbiyik 我相信我了解如何按照您的建议进行操作。我会试一试,看看效果如何。谢谢
  • @OmerTekbiyik,我认为使用 UIView 时无法将 viewController 添加为子视图
  • 关于自定义过渡的 Apple 文档:developer.apple.com/documentation/uikit/animation_and_haptics/… 还有一些不错的 WWDC 视频。

标签: ios swift cocoa-touch uicollectionview


【解决方案1】:

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

然后,将其视图添加为子视图,使用 animateWithDuration. 对其进行动画处理

【讨论】:

  • 我试试看。谢谢。
猜你喜欢
  • 2012-12-27
  • 1970-01-01
  • 2014-10-13
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-08
相关资源
最近更新 更多