Search code examples
iosswiftuicollectionviewuistoryboarduistackview

Want to resize Horizontal StackView that has CollectionView and a Button inside it


I am using a Horizontal CollectionView and UIButton embedded inside a Horizontal StackView. Button has a fixed Height and Width (say 50 X 50 )I want Stackview Width to be dynamic according to CollectionView contentsize. Please guide me through this.

This is what I am Getting

This is what i want to Achieve

StackView Width should grow dynamially


Solution

  • Better way is to add cell in the last index of UICollectionView.

    Here is the implementation

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return COUNT + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.row = COUNT{
          return PLUSCELL
        }else{
          return NORMALCELL
        }
    
    }