Uistackview Hiding/Unhiding Arrangedsubview Issue

Hiding the subview in UIStackview in ios

To answer your first question, if you don't have a need to unhide the subview, the most logical thing to do would be to remove it using removeArrangedSubview(UIView). As you might know, the stack view will automagically update its layout whenever views are added, removed, inserted, or hidden/unhidden.

The warning you're getting in the console about the constraints may or may not be related to whatever you've implemented for the subview right now. Did you mention it because you think it might be related?

Hope that helps.

UIStackView Hide View Animation

Just had the same issue.
The fix is adding stackView.layoutIfNeeded() inside the animation block. Where stackView is the container of the items you're wishing to hide.

UIView.animate(withDuration: DiscoverHeaderView.animationDuration,
delay: 0.0,
usingSpringWithDamping: 0.9,
initialSpringVelocity: 1,
options: [],
animations: {
clear.isHidden = hideClear
useMyLocation.isHidden = hideLocation
completion: nil)

Not sure why this is suddenly an issue in iOS 11 but to be fair it has always been the recommended approach.

UIView hidden is not getting set

Yes, there is a bug / quirk when animating the showing / hiding of arranged subviews in a UIStackView.

You should be able to correct the issue by adding this to your custom view class:

- (void)setHidden:(BOOL)hidden {
if (self.isHidden != hidden) {
[super setHidden:hidden];

Here is a complete example that shows the problem, and shows the "fix": https://github.com/DonMag/StackViewBug

Unhiding subviews in stack view causing weird animation

It turns out it was something to do with animating the update of layouts of views pinned to the edge of the UIStackView. Removing the animation make it work fine.

UIStackView - hide and collapse subview with animation

According to Apple's documentation:

You can animate both changes to the arranged subview’s isHidden property and changes to the stack view’s properties by placing these changes inside an animation block.

I've tested the below code using iOS 12.1 Simulator and it works as expected.

withDuration: 2.0,
delay: 0.0,
options: [.curveEaseOut],
animations: {
self.label.isHidden = true
self.label.alpha = 0.0

Arranged Subview Animation Gif

Hiding a UIStackView with nested UISTackviews

Instead of hiding I think it would be better to remove the stackView itself.


Hope it helps.

Related Topics

Leave a reply