- 我想获得一个deleteButton,它是一个删除图像,位于一个collectionViewCell的外部(它可以部分位于内部)。
Swift iOS-如何将按钮添加到CollectionView单元的外部
我看着其他SO答案和他们说只是与我没有按钮的帧的矩形的
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) }
当我尝试设置了矩形的
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
但我得到的PIC#同样的效果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, :( –