sberrevoets / SDCAlertView

The little alert that could
MIT License
1.86k stars 298 forks source link

How to perform height animation on contentView correctly? #320

Closed yccheok closed 3 years ago

yccheok commented 3 years ago

Thank you for creating SDCAlertView. It enables us to create UIAlertController liked custom view.

I was wondering, is it possible, to animate the height of contentView? Currently, this is what I have done so far.

import UIKit
import SDCAlertView

class ViewController: UIViewController {

    var heightConstraint: NSLayoutConstraint!
    var alert: AlertController!

    func selectClicked() {
        UIView.animate(withDuration: 2) {
            self.heightConstraint.constant = 200
            self.alert.contentView.layoutIfNeeded()
        }
    }

    func customClicked() {

        UIView.animate(withDuration: 2) {
            self.heightConstraint.constant = 300
            self.alert.contentView.layoutIfNeeded()
        }
    }

    @IBAction func buttonClicked(_ sender: Any) {
        print("button clicked")

        let customView = CustomView.instanceFromNib()

        self.alert = AlertController(title: "Title", message: nil, preferredStyle: .actionSheet)

        alert.contentView.addSubview(customView)
        alert.contentView.backgroundColor = UIColor(red: 0.5, green: 0, blue: 0, alpha: 0.1)

        customView.translatesAutoresizingMaskIntoConstraints = false
        alert.contentView.translatesAutoresizingMaskIntoConstraints = false

        customView.heightAnchor.constraint(equalTo: alert.contentView.heightAnchor).isActive = true
        customView.bottomAnchor.constraint(equalTo: alert.contentView.bottomAnchor).isActive = true
        customView.leadingAnchor.constraint(equalTo: alert.contentView.leadingAnchor).isActive = true
        customView.trailingAnchor.constraint(equalTo: alert.contentView.trailingAnchor).isActive = true

        self.heightConstraint = alert.contentView.heightAnchor.constraint(equalToConstant: 300)
        self.heightConstraint.isActive = true

        // So that we can receive buttons click event.
        customView.viewController = self

        alert.addAction(AlertAction(title: "Cancel", style: .normal))

        alert.present()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

Here's the outcome - https://www.youtube.com/watch?v=wg1oEVME4HE

The "parent" of contentView not able to "follow" and animation height change of contentView. The "parent" just change its own height abruptly.

Is there a way, to make the "parent" of contentView also able to animate its own height gracefully?

Thanks.

yccheok commented 3 years ago

Hi,

I able to "solve" the issue by

        UIView.animate(withDuration: 2) {
            self.heightConstraint.constant = 300
            self.alert.contentView.layoutIfNeeded()
        }

to

        UIView.animate(withDuration: 2) {
            self.heightConstraint.constant = 300
            self.alert.view.layoutIfNeeded()
        }