1

I have a UIStackView with the config below, when i hide its first subview i want it to shrink in height rather than redistributing the height of the existing cells to fill or leaving white space.

Is there a way to achieve this? as currently it wont change in height despite hiding its contents

 let headerStackView = UIStackView()
    headerStackView.backgroundColor = .clear
    headerStackView.axis = .vertical
    headerStackView.alignment = .fill
    headerStackView.distribution = .equalSpacing
    headerStackView.spacing = 0
    headerStackView.isUserInteractionEnabled = true
    headerStackView.translatesAutoresizingMaskIntoConstraints = false

Constraints

    headerStackView.snp.makeConstraints { (make) -> Void in
       make.leading.trailing.bottom.equalToSuperview()
       make.top.equalTo(topView.snp.bottom)
    }

Hiding of subview (opposite for showing)

            UIView.animate(withDuration: 0.35) { [weak self] in
            headerStackView.arrangedSubviews.first.isHidden = true
            headerStackView.arrangedSubviews.first.alpha = 0
            self?.detailsHeaderView.headerStackView.layoutIfNeeded()
        }
jwarris91
  • 902
  • 9
  • 24
  • Don't give the stack view a height or bottom constraint. – DonMag Jul 01 '19 at 16:34
  • it has no height, if it doesnt have a bottom constraint it pushed the whole view off the screen, should the content views have heights? – jwarris91 Jul 01 '19 at 16:37
  • looks like that might have resolved that issue actually, the issue im facing seems to be with a superview not resisitng which looked like it was the stackview – jwarris91 Jul 01 '19 at 16:43
  • @DonMag if i remove the bottom constraint, the parent view which pinned to the bottom now cascades due to no height constraint, i cant add height constraint to it as it depends on stackview height, so at what scope do i define height here to make it adapt but fit properly? If i pin the parent view to the stackview bottom, it stops the stackview compressing again – jwarris91 Jul 01 '19 at 16:51
  • You need to show / explain your full view hierarchy. – DonMag Jul 01 '19 at 17:09
  • Parent VC -> UIView -> ContainerUIView -> StackView -> SubViews, now the stackview has no bottom, the container and uiview parent cant get a height and it just cascades – jwarris91 Jul 02 '19 at 08:20
  • Its pretty much a case of what does the container of the stackview clip to if not the bottom of the stackview (as that limits the stackview being able to compress it seems) – jwarris91 Jul 02 '19 at 09:24
  • Unfortunately, it's still not entirely clear what you are trying to do... Is this what you're going for? https://imgur.com/a/OzCP1E9 – DonMag Jul 02 '19 at 12:38

0 Answers0