1

I'm trying to animate in and out a button in a stack view, when one is pressed. When hiding the camera button, the animation looks fine, but when unhiding it slides in from the left which looks odd.

Link to GIF since StackOverflow won't let me embed

Code below:

@IBAction func scanButtonTapped(_ sender: Any) {
    
    
    UIView.animate(withDuration: 0.3) { [self] in
        if cameraButton.alpha == 0{
            cameraButton.alpha = 1
        }else{
            cameraButton.alpha = 0
        }
    }
    
    UIView.animate(withDuration: 0.3) { [self] in
        cameraButton.isHidden.toggle()
        
    }
    
}

I've tried to fix it by using constraints instead of a stack but to no avail. If anyone could help it would be much appreciated. (Please ignore how awful the if/else statement is)

1 Answers1

2

Cheers to @Ibrahimyilmaz. The solution involved just adding stackView.layoutIfNeeded() after isHidden.toggle()

enter image description here

Some more infomation on this courtesy of @aheze