简体   繁体   中英

Hiding an arrangedSubview in animation block UIStackView

I am trying to create an Expandable View using stack view, very easily so when the user clicks on the first arrangedSubview of the StackView the second one's isHidden property is set to false and hence it animates.

The functionality works, but the animation is way off. Every time the isHidden property is set to false, the arranged subview animates not from below the first arrangedSubview, but from the top of the screen instead.

Any ideas on how to fix that? I am using this simple piece of code, as described in Apple's documentation for UIStackView:

UIView.animate(withDuration: animationDuration) {
        let bodyView = self.stackView.arrangedSubviews[1]
        bodyView.isHidden = !self.isExpanded
        self.expandArrowImageView.transform = CGAffineTransform(rotationAngle: angle)
    }

You need to invoke the code after the view has appeared, ie in viewDidAppear. The animation behaviour you are seeing now happens when you attempt to invoke it before the view has appeared, ie in viewDidLoad

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM