2016-10-30 2 views
0

私はすべてのスクリーンサイズに自分のアプリケーションを合わせようとしていますが、どうやって解決するのか分かりません。画面に応じてセルのサイズを変更することは可能ですか?現時点では、私はUICollectionViewを以下のように設定しています:すべての画面に合わせてセルサイズを調整しますか?

func numberOfSections(in collectionView: UICollectionView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of items 
     return fields.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! FieldDistributionCollectionViewCell 


     cell.frame.size = CGSize(width: ?, height: ?) 
     return cell 
    } 

推奨方法はありますか?または、電話機のバージョンを確認して手動でサイズを設定する必要がありますか?小さな画面(4,4s)では、私は2列しか必要ではなく、(5,5s、6,6s)に3列が必要です。基本的には、セルのサイズを適切な解像度に合わせるだけです。

+0

スクリーンショットhttp://stackoverflow.com/questions/40131349/enforce-collectionview-to-have-only-2-rows/40133928#40133928 – Joe

+0

と私の答えを見しかし、あなたのアプローチと私は」常に2つの列を取得します。それはまさに私が達成したいものではありません。 – Recusiwe

+0

あなたが望む列数... – Joe

答えて

1

は、このコードを試してみてください:

がcollectionViewのプロトコルから以下の機能を実装します。ViewDidLayoutSubviewsでは(これは、あなたのフレーム幅を知っているとき)、幅と高さを設定

// cell size 
func collectionView(collectionView: UICollectionView, 
    layout collectionViewLayout: UICollectionViewLayout, 
    sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

    // To get 1 column 
return CGSize(width: view.frame.size.width, height: view.frame.size.width) 

// To get 2 column 
//return CGSize(width: view.frame.size.width/2, height: view.frame.size.width/2) 

// To get 3 column 
// return CGSize(width: view.frame.size.width/3, height: view.frame.size.width/3) 

// Toget 4 column 
// return CGSize(width: view.frame.size.width/4, height: view.frame.size.width/4) 
    } 

を...あなたのコントローラの(スーパー)ビューはどこですか?

// inter-spacing 

func collectionView(collectionView: UICollectionView, 
    layout collectionViewLayout: UICollectionViewLayout, 
    minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat { 
return 2.0 
} 

// line-spacing 

func collectionView(collectionView: UICollectionView, layout 
    collectionViewLayout: UICollectionViewLayout, 
    minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat { 
return 2.0 
} 

CollectionView layoあなたの原則。

enter image description here

+0

私は見ていますが、電話機やスクリーンサイズのモデルを決定する必要があります。 – Recusiwe

0

interface builderのドキュメントアウトラインからcollectionViewFlowLayoutをコードにドラッグしてコンセントを作成します。

//MARK: IBOutlet 
    @IBOutlet weak var collectionViewFlowLayout: UICollectionViewFlowLayout! 

    override func viewDidLayoutSubviews() { 
     super.viewDidLayoutSubviews() 
     let width = collectionView.frame.size.width - collectionViewFlowLayout.sectionInset.left - collectionViewFlowLayout.sectionInset.right 
     collectionViewFlowLayout.itemSize = CGSize(width: width, height: width) 
    } 
+0

これは私が望むものを作り出しますか? ?異なる画面上の列の数だけではありませんか? – Recusiwe

+0

各画面に1つの列があり、セルは正方形です。他のアスペクト比が必要な場合は、数学を行います。コードを読んでください。コレクションビューのセルは、コレクションビューと同じサイズで、設定したマイナスおよび左と右のマージンのパディングです。 –