13

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?

Daniel
  • 20,420
  • 10
  • 92
  • 149
  • Have you tried a UIView performWithoutAnimation: block? – titaniumdecoy Feb 16 '16 at 17:49
  • I don't want an animation, I want to disable the animation that is happening – Daniel Feb 16 '16 at 17:53
  • I know... that's why I suggested you try perform*Without*Animation – titaniumdecoy Feb 16 '16 at 17:54
  • Sorry, I read `performWithAnimation`, I didn't even know `performWithoutAnimation` existed, but unfortunately it does not work. – Daniel Feb 16 '16 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 '16 at 18:12
  • No problem. I'm glad it worked for you. – titaniumdecoy Feb 16 '16 at 18:15
  • @simpleBob how did it work for you? I just add stackView.addArrangedSubview(subview) in the performWithoutAnimation: block, but it still animates. – yeesterbunny Jun 01 '16 at 23:06
  • @yeesterbunny I solved it specifically for my case, but putting `layoutIfNeeded` in the `UIView.performWithoutAnimation` block instead should work – Daniel Jun 02 '16 at 08:58
  • @simpleBob That was eventually what I did, and it worked! I wouldn't had solved it without seeing your post. Thanks! – yeesterbunny Jun 02 '16 at 17:58
  • @Daniel did you put `stackView.layoutIfNeeded()` or your stack view's superview `layoutIfNeeded()` in the block? One of my arranged subviews is still animating. – Clay Ellis May 11 '17 at 18:55

2 Answers2

27

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.

Womble
  • 4,607
  • 2
  • 31
  • 45
  • from @mrfonda: `Tested on iOS11, Xcode 9.0 (9A235)` – Daniel Oct 10 '17 at 07:58
  • After upgrading to iOS11 my app had begun animating. This code helped me preventing the unwanted animation. Xcode 9.0.1 (9A1004) – neoneye Nov 02 '17 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 '18 at 13:00
9

UIView performWithoutAnimation: will execute its block argument without animation:

UIView.performWithoutAnimation {
    stackView.addArrangedSubview(subview)
}
titaniumdecoy
  • 18,900
  • 17
  • 96
  • 133
  • 1
    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 '17 at 04:23