【发布时间】:2017-06-08 05:11:19
【问题描述】:
- 我想要一个 deleteButton,它是一个删除图像,位于 collectionViewCell 的外部(它可以部分位于内部)。
-
我查看了其他 SO 答案,他们说只需使用我所做的按钮框架的 Rect 的
x & y值。当我将x & y值设置为0,0时,我得到:class CustomCell: UICollectionViewCell { @IBOutlet weak var imageView: UIImageView! var deleteButton: UIButton! var deleteButtonImg: UIImage! override func awakeFromNib() { super.awakeFromNib() deleteButton = UIButton(frame: CGRect(x: 0, y: 0, width: frame.size.width/4, height: frame.size.width/4)) deleteButtonImg = UIImage(named: "delete-icon")!.withRenderingMode(.alwaysTemplate) deleteButton.setImage(deleteButtonImg, for: .normal) deleteButton.tintColor = UIColor.red contentView.addSubview(deleteButton) }
-
当我尝试将 Rect 的
x & y设置为-10,-10时,deleteButton 会被剪切到单元格的 imageView 内。我没有设置clipsToBounds。deleteButton = UIButton(frame: CGRect(x: -10, y: -10, width: frame.size.width/4, height: frame.size.width/4))
我什至尝试将clipToBounds 设置为false,但我得到的效果与图片#3 相同。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.imageView.image = myImageArray[indexPath.row]
cell.imageView.clipsToBounds = false
return cell
}
我哪里出错了?
【问题讨论】:
-
尝试在自定义单元格中设置
deleteButton.clipsToBounds = false -
@Shankar BS 感谢您的帮助 :) 在“class CustomCell: UICollectionViewCell”中,我尝试了 deleteButton.clipsToBounds = false,然后尝试了 imageView.clipsToBounds = false,但都没有成功:(
标签: uibutton frame uicollectionviewcell cgrect bounds