2017-09-27 5 views
1

コレクションセルのサイズをプログラムのサイズに合わせようとしていますが、アプリケーションを実行するとセルサイズは変わりません。これはSwift 4とXcode 9を呼び出すための適切な機能ですか?Swift 4 - クラス 'NSObject'と 'UICollectionViewFlowLayout'からの多重継承

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) 

    } 
} 

編集:

このテーブルビューは、ページの下部からアニメーションやポップアップメニューを使用しているビューコントローラから呼び出されているコレクションビューを提示されています。

私は今のエラーを取得:

Multiple inheritance from classes 'NSObject' and 'UICollectionViewFlowLayout'

+0

にメソッドが呼び出されますか?対応する代理人を設定しましたか? – Larme

+0

すべてのコードを表示するように更新しました –

答えて

4

UICollectionViewFlowLayoutは、プロトコルではありません、それはクラスです。 UICollectionViewFlowLayoutの代わりにUICollectionViewDelegateFlowLayoutを使用する必要があります。

変更

class SettingsLauncher: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewFlowLayout 

class SettingsLauncher: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout 
+0

パーフェクト、よろしくお願いいたします –