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

Nia*_*dle 2 uicollectionview uicollectionviewcell swift 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)

    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

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

我现在得到错误:

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

Tam*_*gel 10

UICollectionViewFlowLayout不是协议,它是一个类.你需要使用UICollectionViewDelegateFlowLayout而不是UICollectionViewFlowLayout.

更改

class SettingsLauncher: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewFlowLayout
Run Code Online (Sandbox Code Playgroud)

class SettingsLauncher: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
Run Code Online (Sandbox Code Playgroud)