0

I'am observing data in firebase, when this data changed, the collectionview reloads it's data, where I remove a label from the vertical stackview, I want to add this label again to the vertical stackview during runtime on some conditions, the vertical stackview is a part of a collectionview cell.

override func layoutSubviews() {
 if(serviceUserCompany.text == ""){
           detailsStackView.removeArrangedSubview(serviceUserCompany)
           serviceUserCompany.isHidden = true
       }

       if(serviceUserCompany.text != "" && !detailsStackView.arrangedSubviews.contains(serviceUserCompany)){
           detailsStackView.insertArrangedSubview(serviceUserCompany, at: 2)
       }
}
  • What is your question? Instead of removing the subView, just hide it instead. If the label is part of the collectionView cell, put that code in cellForItem. – rs7 Jun 05 '20 at 21:42
  • As chedabob said, `viewDidLayoutSubviews` is not the right place to do this. And, FYI, it’s prudent to call `super` rendition if you `override` any of these appearance related methods. – Rob Jun 05 '20 at 22:27
  • By the way, if you’re going to `removeArrangedSubview` and hide it, when adding it back, you presumably want to unhide it. – Rob Jun 05 '20 at 22:30

1 Answers1

0

Hide it instead of removing it. It'll be reduced to 0 height, and the other views will move up to take its place.

Also don't update your views in layoutSubviews. It'll cause all sorts of weird issues.

chedabob
  • 5,835
  • 2
  • 24
  • 44