2016-12-18 12 views
2

私はこのような単純なcollectionviewコントローラがあります。そこだけuiimageviewがあり、それは、この制約を有する細胞ではUIcollectionviewセルの問題スウィフト

class ViewController1: UICollectionViewController{ 

    override func viewDidLoad() { 

     super.viewDidLoad() 

     self.collectionView?.delegate = self 
     self.collectionView?.dataSource = self 

    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

     return 5 
    } 

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "P", for: indexPath) 

     return cell 
    } 
} 

を:

enter image description here

私の問題はこれです:

私はiPhone 6シミュレータでこのコードを実行すると、これを(正しい方法で)得る: enter image description here

が、私はiPhone 5用シミュレータ上でこのコードを実行するとき、私はこの(間違った方法:ImageViewのは左の画面の前に始まり、幅と高さが制約に間違った点ですが):取得 enter image description here

私はこの問題は狂っていますが、私は解決できません。 私を助けることができますか?

+0

xcodeプレビューでこれをチェックしましたか?さまざまなデバイスを追加することで、正確な制約がどのようなものかを知ることができます:http://pinkstone.co.uk/how-to-preview- storyboards-in-interface-builder/ –

+0

私はこれを試しましたが、シミュレータで同じものが表示され、エラーは何か分かりません – gianni

+0

コレクションビューの制約を追加しようとしましたか?だから常にスーパービューの内側になるでしょう –

答えて

1

ViewController1UICollectionViewDelegateFlowLayoutプロトコルに準拠するように拡張する必要があります。

class ViewController1: UICollectionViewController, UICollectionViewDelegateFlowLayout { 
override func viewDidLoad() { 

    super.viewDidLoad() 

    self.collectionView?.delegate = self 
    self.collectionView?.dataSource = self 
} 

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 

    print(self.collectionView!.frame) 
} 

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    return 5 
} 

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "P", for: indexPath) 

    return cell 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 
    return 0 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
    return 0 
} 

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

それはハードの値をコード化しているが、あなたのアイデアを得る必要がありますので、同様に各コレクションビューセルのサイズを設定するメソッドを実装します。コレクションビューの幅は各アイテムの最大サイズにする必要があります。ボックスの外に出ないようにします。

関連する問題