iOS Dynamically updating a UIStackView Connect the UISwitch to an action we can animate switching between a horizontal or vertical layout of the image views

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

@IBAction func axisChange(sender: UISwitch) {
    UIView.animateWithDuration(1.0) {
        self.updateConstraintsForAxis()
    }
}

The updateConstraintForAxis function just sets the axis of the stack view containing the two image views:

private func updateConstraintsForAxis() {
    if (axisSwitch.on) {
        stackView.axis = .Horizontal
    } else {
        stackView.axis = .Vertical
    }
}

The animated gif below gives you an idea of how this appears:

enter image description here



Got any iOS Question?