2017-02-03 6 views
0

目的:すべてのサイズのiPhoneで2つのセルを持つエッジツーエッジUICollectionView。UICollectionViewエッジからエッジへのレイアウト

iPhone 6上しかし
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

    let screenSize = collectionView.bounds 
    let screenWidth = screenSize.width 
    print("Zhenya: \(screenWidth)") // prints correct 375, which is half of iPhone 6 
    let cellEdgeLength: CGFloat = screenWidth/2.0 

    return CGSize(width: cellEdgeLength, height: cellEdgeLength) 
} 

また

override func viewDidLoad() { 
    super.viewDidLoad() 
    collectionView.delegate = self 
    collectionView.dataSource = self 

    let flow = UICollectionViewFlowLayout() 
    flow.scrollDirection = UICollectionViewScrollDirection.vertical 
    flow.minimumInteritemSpacing = 0 
    flow.minimumLineSpacing = 0 
    collectionView.collectionViewLayout = flow 
} 

enter image description here

コレクションセルの属性:

enter image description here

コレクションビュー属性:

enter image description here

更新:グラデーションの FUNC、実際には右の幅を取得: (カスタムUICollectionViewCellクラスに位置)

func addGradient() { 

    let gradient = CAGradientLayer() 
    gradient.frame = gradientView.bounds 
    let topColor = UIColor(red:0.07, green:0.07, blue:0.07, alpha:1) 
    let botomColor = UIColor.clear 
    gradient.colors = [topColor.cgColor, botomColor.cgColor] 

    if gradientWasRemoved == false { 
    gradientView.layer.insertSublayer(gradient, at: 0) 
    } else if gradientWasRemoved == true { 
     self.addSubview(gradientView) 
    } 

アップデート2: 注:iPhone 7 Plusでのテスト。 私はこのコードで (それのように思える)UICollectionViewFlowLayoutがsizeForItemAtIndexPath:にcratedセルサイズをオーバーライドすることがわかった:

let flow = UICollectionViewFlowLayout() 
    let screenSize = collectionView.bounds 
    let screenWidth = screenSize.width 
    let cellEdgeLength: CGFloat = screenWidth/2.0 

    flow.itemSize = CGSize(width: cellEdgeLength, height: cellEdgeLength) 

    flow.scrollDirection = UICollectionViewScrollDirection.vertical 
    flow.minimumInteritemSpacing = 0 
    flow.minimumLineSpacing = 0 
    collectionView.collectionViewLayout = flow 

私はこれがあります。

enter image description here

をそれから私は、マニュアル(セル端の長さを指定することを決めましたiPhone7Plustの幅の半分= 207):

let cellSide: CGFloat = 207 
flow.itemSize = CGSize(width: cellSide, height: cellSide) 

flow.scrollDirection = UICollectionViewScrollDirection.vertical 
flow.minimumInteritemSpacing = 0 
flow.minimumLineSpacing = 0 
collectionView.collectionViewLayout = flow 

これは:

enter image description here

+1

あなたの試したCGSize(width :(self.collectionView!.frame.width/2)、height:(self.collectionView!.frame.width/2)) 'を返すのですか? frame =親ビューの座標系を使用するビューの位置とサイズ bounds =ビューの位置とサイズが独自の座標系を使用 – mat

+0

動作しませんでした。 いくつかの手がかりを与えることができます - 私は自分のセルにグラデーションビューを持っており、それは正しい幅を取る。私は勾配のコードで自分の投稿を更新します。多分それが助けになるでしょう。 –

+0

かもしれません。あなたはその勾配ビューに制約があるのがわかります – mat

答えて

0

解決策を見つけました。 内部の画像ビューの自動レイアウト設定に問題がありました。私はそれらを指定しなかった。 なぜBSの小さな部分が私の流れの途中になってしまったのですか。サイズ、私は分かりません。 何が私にとって奇妙なのですか?2番目の更新で207というように手動でcellEdgeLengthを指定すると、突然、imageView制約の不足がオーバーライドされました。