如何在uicollectionview单元格中调整imageview的位置?

时间:2016-07-11 23:58:06

标签: ios swift uiimageview uicollectionview uicollectionviewcell

我在UICollectionView单元格中有一个UIImageView,其大小我是根据屏幕大小以编程方式调整的。单元格创建两列,我希望在查看集合时图像更接近屏幕中心。

override func collectionView(collectionView: UICollectionView,    cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ProfileCollectionViewCell

    cell.myLabel?.text = fetchName()
    cell.myImage?.image = fetchImage()

    if(indexPath.item % 2 == 0) {
        cell.myImage?.frame.origin.x = (cell.frame.size.width / 3) * 2
        cell.myImage?.frame.origin.y = cell.frame.size.height / 2
    }
    else {
        cell.myImage?.frame.origin.x = (cell.frame.size.width / 3)
        cell.myImage?.frame.origin.y = cell.frame.size.height / 2
    }

    cell.myImage?.layer.cornerRadius = (cell.myImage?.frame.size.width)! / 2
    cell.myImage?.layer.masksToBounds = true
    return cell
}

但这似乎并没有实际移动图像视图。我也尝试过调整cell.myImage?.center,但这也没有任何影响。那么如何在这个单元格周围移动图像呢?

1 个答案:

答案 0 :(得分:0)

尝试在imageView ProfileCollectionViewCell方法中调整layoutSubviews的位置,这是您想要实现目标的地方

我希望这有助于你

相关问题