0
votes

I have a scenario that I have X Labels (or Buttons). They should appear in a horizontal collection view. There shouldn't be scrollable. The thing is that the number of lables/buttons can change (0-5).

The perfect thing would be to use a StackView with equal width for every item but I can't use that, because it needs iOS 9. (I need compatibility for iOS 8.0 too -.-)

So I build a collection view, disabled to scroll and want to set a dynamically height and width for my cells.

I get the height/width of my collectionView

heightOfLabel = testModeLoginCollectionView.frame.height
widthOfLabel = testModeLoginCollectionView.frame.width / 
                   CGFloat(array5.count)

The results are testModeLoginCollectionView.frame.width = 568.0 widthOflabel 113.6 array5 = 5 items

I used this method to size my cells:

 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

     let c = CGSizeMake(heightOfLabel!, widthOfLabel!);
     return c
  }

He should size the cells to width 113.6 and 33.0 But it looks like this: enter image description here

I tried a little bit with Aspect Fit, Scale to Fill etc. but it doesn't change so much. It is too big or too small. enter image description here

Does anybody has an idea? I would use another technology than collection View, too.

when did i store the values in my variables?

override func viewDidLoad() {

    testModeLoginCollectionView.delegate = self
    testModeLoginCollectionView.dataSource = self

    heightOfLabel = testModeLoginCollectionView.frame.height
    widthOfLabel = testModeLoginCollectionView.frame.width / CGFloat(array5.count)
    print("testModeLoginCollectionView.frame.width \(testModeLoginCollectionView.frame.width)")
    print("widthOfLabel \(widthOfLabel)")

}

I tried it in viewDidAppear to, but no difference

1
At what point are you storing the values for heightOfLabel and widthOfLabel?ZeMoon

1 Answers

1
votes

after viewDidLayoutSubviews view have right frame. so you can in viewDidLayoutSubviews reloadData. but i have another solution:you could use https://github.com/forkingdog/FDStackView it's have same api like uistackview