Swift: Disappearing views from a stackView
<p>I have a fairly simple set up in my main storyboard:</p>

<ul>
<li>A stack view which includes three views</li>
<li>The first view has a fixed height and contains a segment controller</li>
<li>The other two views have no restrictions, the idea being that only one will be active at a time and thus fill the space available</li>
</ul>

<p>I have code that will deal with the changing view active views as follows:</p>

<pre><code>import Foundation
import UIKit
class ViewController : UIViewController {
    @IBOutlet weak var stackView: UIStackView!
    @IBOutlet weak var segmentController: UISegmentedControl!
    @IBAction func SegmentClicked(_ sender: AnyObject) {
        updateView(segment: sender.titleForSegment(at: sender.selectedSegmentIndex)!)
    }
    override func viewDidLoad() {
        updateView(segment: "First")
    }
    func updateView(segment: String) {
        UIView.animate(withDuration: 1) {
            if(segment == "First") {
                self.stackView.arrangedSubviews[1].isHidden = false
                self.stackView.arrangedSubviews[2].isHidden = true
            } else {
                self.stackView.arrangedSubviews[1].isHidden = true
                self.stackView.arrangedSubviews[2].isHidden = false

            }
            print("Updating views")
            print("View 1 is \(self.stackView.arrangedSubviews[1].isHidden ? "hidden" : "visible")")
            print("View 2 is \(self.stackView.arrangedSubviews[2].isHidden ? "hidden" : "visible")")
        }
    }
}
</code></pre>

<p>As you can see, when the tab called 'First' is selected, the subview at index 1 should show, whilst 2 is hidden, and when anything else is selected, the subview at index 2 should show, whilst 1 is hidden.</p>

<p>This appears to work at first, if I go slowly changing views, but if I go a bit quicker, the view at index 1 seems to remain permanently hidden after a few clicks, resulting in the view at index 0 covering the whole screen. I've placed an animation showing the issue and a screenshot of the storyboard below. The output shows that when the problem happens, both views remain hidden when clicking on the first segment.</p>

<p>Can anybody tell me why this is happening? Is this a bug, or am I not doing something I should be?</p>

<p>Many thanks in advance!</p>

<p><a href="https://i.stack.imgur.com/gQqCi.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/gQqCi.gif" alt="Animation showing issue"></a></p>

<p><a href="https://i.stack.imgur.com/sWTYS.png" rel="noreferrer"><img src="https://i.stack.imgur.com/sWTYS.png" alt="Image of stack view in storyboard"></a></p>

<p>Update: I seem to be able to reliably reproduce the issue by going to the First > Second > Third > Second > First segments in that order.</p>