2017-06-08 265 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

    歡迎你。 :)很高興你找到了答案 –

    相關問題