Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to perform height animation on contentView correctly? #320

Closed
yccheok opened this issue Jun 29, 2021 · 1 comment
Closed

How to perform height animation on contentView correctly? #320

yccheok opened this issue Jun 29, 2021 · 1 comment

Comments

@yccheok
Copy link
Contributor

yccheok commented Jun 29, 2021

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
Copy link
Contributor Author

yccheok commented Jun 29, 2021

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()
        }

@yccheok yccheok closed this as completed Jun 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant