ios - 从 UICollectionViewController 制作一个 Popup ViewController

标签 ios swift cocoa-touch uicollectionview

我正在使用 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
}

最佳答案

一种方法是使用 view controller containment.您将在 didSelectRow 中实例化 ChapterNumbersCollectionView,但不是呈现,而是将其添加为 subview Controller 。

然后,将其 View 添加为 subview 后,使用 animateWithDuration. 对其进行动画处理。

关于ios - 从 UICollectionViewController 制作一个 Popup ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54524397/

相关文章:

Swift:在字符串文字中提供默认值

swift - AVCapturePhotoOutput 不提供预览缓冲区

ios - 需要将 NSMutableString 对象从 View Controller 传递给 AppDelegate

ios - Linkedin ios SDK createSessionWithAuth更新错误

swift - 如何在 Swift 中添加 SKSpriteNode 到场景中?

cocoa-touch - 如何使用触摸移动 UIImageView

objective-c - 使用 sortUsingComparator 对 NSInteger 进行排序

ios - 压缩视频的尺寸/秒转换率是多少

objective-c - 是否可以有一个仅在该方法尚不存在时才加载的 Objective-C 类别?

ios - 防止调用 API 获取时间范围内的数据