2016-11-22 195 views
0

如何使收集視圖單元的內容偏移。下面的代碼我有什麼,但它不會出於某種原因:收集視圖單元的內容偏移

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "pCell", for: indexPath) as! CollectionViewCell 
    cell.imageView.image = UIImage(named: "1")! 

    cell.offset(offset: CGPoint(x: 0.0, y: -30.0)) 

    return cell 
    } 

我的手機類代碼:

class CollectionViewCell: UICollectionViewCell { 

    @IBOutlet weak var imageView: UIImageView! 
    var image: UIImage = UIImage() { 
    didSet{ 
     self.imageView.image = image 
    } 
    } 

    func offset(offset: CGPoint) { 
    self.imageView.frame = self.imageView.bounds.offsetBy(dx: offset.x, dy: offset.y) 
    } 
} 

我只是不明白的顯示單元格時,任何偏移。我做錯了什麼,夥計?

+0

除非它是你的子類特定的東西,否則'UICollectionViewCell'沒有名爲'offset'的屬性。 – Wes

+0

@是的,我更新了我的問題。 –

+0

你有沒有對'imageView'應用任何約束? – Wes

回答

1

確保您沒有在imageView上啓用任何IB限制,它們將防止您更新框架。