Swift 4 - 来自类的多重继承' NSObject'和' UICollectionViewFlowLayout'

时间:2017-09-27 15:12:27

标签: swift uicollectionview uicollectionviewcell swift4

我正在尝试以编程方式将收集单元格的大小设置为框架的宽度,但是,当我运行应用程序时,单元格大小不会改变。这是调用Swift 4和Xcode 9的正确功能吗?

import UIKit

class SettingsLauncher: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewFlowLayout {    
    let blackView = UIView()

    let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor.white
        return cv
    }()

    let cellID = "cell"

    @objc func showMenu() {
        // Show menu

        if let window = UIApplication.shared.keyWindow { // Get the size of the entire window

            blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)

            let height: CGFloat = 200
            let y = window.frame.height - height // The y value to appear at the bottom of the screen
            collectionView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: height)

            // Add gesture recognizer on black view to dismiss menu

            blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))

            window.addSubview(blackView)
            window.addSubview(collectionView)

            blackView.frame = window.frame
            blackView.alpha = 0

            // Slow the animation down towards the end (curveEasOut)
            UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                // Animate black view
                self.blackView.alpha = 1

                // Animate collection view
                self.collectionView.frame = CGRect(x: 0, y: y, width: self.collectionView.frame.width, height: height)
            }, completion: nil)
        }
    }

    @objc func handleDismiss() {
        // Dimisses menu view

        UIView.animate(withDuration: 0.5) {
            self.blackView.alpha = 0

            if let window = UIApplication.shared.keyWindow {

                self.collectionView.frame = CGRect(x: 0, y: window.frame.height, width: self.collectionView.frame.width, height: self.collectionView.frame.height)
            }
        }
    }

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

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

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: 50)
    }

    override init() {
        super.init()

        collectionView.delegate = self
        collectionView.dataSource = self

        collectionView.register(MenuCell.self, forCellWithReuseIdentifier: cellID)

    }
}

修改

此表格视图正在从页面底部进行动画处理,并显示一个集合视图,该视图是从使用弹出菜单的视图控制器调用的。

我现在收到错误:

  

来自班级'NSObject''UICollectionViewFlowLayout'

的多重继承

1 个答案:

答案 0 :(得分:7)

UICollectionViewFlowLayout不是协议,而是一个类。您需要使用UICollectionViewDelegateFlowLayout代替UICollectionViewFlowLayout

更改

class SettingsLauncher: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewFlowLayout

class SettingsLauncher: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout