Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I add views to a TZStackView (which should be basically the same as a UIStackView , but with a fallback for older versions) like this:

stackView.addArrangedSubview(subview)

the stackView and the subview are of dynamic size (auto-layout) and resize themselves. However, this happens with an animation (I think due to addArrangedSubview, it does not happen if I add it as subview and set constraints). Is there a way to deactivate the animations?

Sorry, I read performWithAnimation, I didn't even know performWithoutAnimation existed, but unfortunately it does not work. – Daniel Feb 16, 2016 at 18:01 It actually DOES work. Please write it as an answer so I can accept it ;) and thanks a lot, I have been searching for something that works the whole day :) – Daniel Feb 16, 2016 at 18:12

Disabling animations while adding arranged views to a UIStackView does not work properly in iOS 9.

The solution is to perform the addition separately from the layout:

// Add all views first.
for view in views {
    stackView.addArrangedSubview(view)
// Now force the layout in one hit, sans animation.
UIView.performWithoutAnimation {
    stackView.setNeedsLayout()
    stackView.layoutIfNeeded()

Tested on iOS 9.3, Xcode 8.3.2. Not tested on iOS 10 or 11 yet.

After upgrading to iOS11 my app had begun animating. This code helped me preventing the unwanted animation. Xcode 9.0.1 (9A1004) – neoneye Nov 2, 2017 at 15:09 stackView.addArrangedSubview(button01) stackView.insertArrangedSubview(button02, at: 0) UIView.performWithoutAnimation { self.view.layoutIfNeeded() } worked for me tested in iOS 11 and 12 – Jasmin Nov 15, 2018 at 13:00

UIView performWithoutAnimation: will execute its block argument without animation:

UIView.performWithoutAnimation {
    stackView.addArrangedSubview(subview)
                This doesn't work in Xcode 8.3.2, iOS 9.3. Regardless of where you add the 'performWithoutAnimation' call, there is still some animation happening. It just changes the nature of the animation. I regard this as a bug and a mis-featured API. See new Answer for a solution in iOS 9.
– Womble
                Jun 20, 2017 at 4:23
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.