user12408331
user12408331

Reputation:

Why is my button not displaying in the iphone simulator but showing in storyboard

I'm trying to display a button in a collection view, but it's not showing when I run it on the iphone simulator; however it clearly shows in the storyboard (refer to image). Also - I notice that when I add horizontal and vertical constraints it shows. When i delete the constraints however, the button completely disappears. Shouldn't some of the button display in the collection view since my coordinates don't seem to be off enough for the entire button to not display? Sorry if this is a lousy question - I just picked up ios development.

Storyboard

Simulator

Upvotes: 0

Views: 188

Answers (1)

Frankenstein
Frankenstein

Reputation: 16341

Your UIButton seems to be getting smaller due to the auto-resizing of UICollectionViewCell. To fix the issue you can either set the itemSize property of the collectionViewLayout or by returning the sizeForItem if you're giving different size for each cell. Here's the code:

let flowLayout = UICollectionViewFlowLayout()
flowLayout.itemSize = CGSize(width: collectionView.frame.width, height: 300)
collectionView.collectionViewLayout = flowLayout

Here's how you add it via Size Inspector:

  1. Select the Collection View Flow Layout on the left
  2. Then in the size inspector tab on the right you can give Cell Size.

enter image description here

Upvotes: 1

Related Questions