2017-06-08 276 views
0
  1. 我想获得一个deleteButton,它是一个删除图像,位于一个collectionViewCell的外部(它可以部分位于内部)。

enter image description hereSwift iOS-如何将按钮添加到CollectionView单元的外部

  • 我看着其他SO答案和他们说只是与我没有按钮的帧的矩形的x & y值播放。当我设置了x & y0,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) 
    } 
    
  • enter image description here

  • 当我尝试设置了矩形的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)) 
    
  • enter image description here

    我甚至试着设置clipToBoundsfalse但我得到的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 
        } 
    

    我要去哪里错了?

    +0

    尝试在自定义单元格中设置'deleteButton.clipsToBounds = false' –

    +0

    @Shankar BS感谢您的帮助:)在“class CustomCell:UICollectionViewCell”中我尝试了deleteButton.clipsToBounds = false,然后尝试imageView.clipsToBounds = false, :( –

    回答

    1

    问题是故事板里面我有一个:

    CollectionView 
         -CustomCell (type collectionViewCell) 
          -imageView 
    

    在属性检查器中,在牵引部,所有这3具有这样的性质clipsToBounds

    我忽略了一个事实,即clipToBounds在imageView上设置为false(未选中),但在CustomCell和CollectionView上设置为true(选中)。

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    如果任何人有这个问题做出了的CollectionView,collectionViewCell务必uncheck clipsToBounds和ImageView的。

    +0

    很好的答案,只是一个侧面说明 - collectionview中的clipToBounds将剪切x按钮的顶部,单元格上的clipToBounds将剪切x按钮的前端.. – Eyzuky

    +0

    @Eyzuky感谢您的额外信息。一切都有用! –

    1

    好u能做到像下面,首先创建一个XIB文件,并替换集合视图细胞缺省视图,设置如下图所示,

    enter image description here

    上述图像中设置的类名CustomCell(在我的情况),并放置图像视图(绿色)和按钮(蓝色)。

    View controller

    override func viewDidLoad() { 
        super.viewDidLoad() 
        // Do any additional setup after loading the view, typically from a nib. 
        //register the nib to collectionview 
        self.aColectionView.register(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "CUSTOM_CELL") 
    } 
    
    override func didReceiveMemoryWarning() { 
        super.didReceiveMemoryWarning() 
        // Dispose of any resources that can be recreated. 
    } 
    
    func numberOfSections(in collectionView: UICollectionView) -> Int { 
        return 1 
    } 
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
        return 40 
    } 
    
    //cell creation as u are doing 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
        let cell:CustomCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CUSTOM_CELL", for: indexPath) as! CustomCell 
        //set the image for custom cell 
    
        return cell 
    } 
    
    //return the required size 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
        return CGSize(width: 200, height: 150) 
    } 
    

    和输出象下面,

    enter image description here

    和定制细胞类中,u添加像一个按钮动作和其它视图相关的代码的任何其它特征

    class CustomCell:UICollectionViewCell { 
    
        @IBOutlet weak var deleteButton: UIButton! 
        @IBOutlet weak var imageView: UIImageView! 
    
    
        //other view related code 
    } 
    
    +0

    明天我会试试,我即将入睡,我会让你知道它是如何工作的谢谢顺便说一句,第二张图片没有显示 –

    +0

    我找到了答案,在故事板中我有imageView的clipsToBounds但没有选中,但collectionViewCell和collectionView的clipsToBounds被检查。我要为你的答案投票,因为这是一个很好的选择!再次感谢:) –

    +0

    欢迎你。 :)很高兴你找到了答案 –

    相关问题