Adding a UIButton to a UICollectionView cell programmatically

后端 未结 1 1207
天命终不由人
天命终不由人 2021-01-05 21:24

I have a UIViewController with a UICollectionView created inside it programmatically. I want to add a button to the cell:

viewDidLoad:



        
1条回答
  •  北荒
    北荒 (楼主)
    2021-01-05 21:51

    Add your button as a subview of cell.contentView. Also, don't create the button every time collectionView:cellForItemAtIndexPath: is called. You might be reusing an existing cell that already has a button. Better to add the button in your custom cell's init method instead. Then just hide the button when you don't need it.

    0 讨论(0)
提交回复
热议问题