Friday, November 25, 2022
HomeiOS Developmentios - Custom alert view display - Dismiss doesn't work - Swift

ios – Custom alert view display – Dismiss doesn’t work – Swift

[ad_1]

I tried to display a custom alert view when an email address is incorrect, everything works fine, the view is displayed when the email address isn’t valid, except the dismiss button, can somebody help me ? The view doesn’t disappear. I literally don’t know why it doesn’t work, probably a dumb mistake. Ive spent hours on it. Thanks

The code :

public class MyAlert: NSObject {
    
    private let backgroundView: UIView = {
        let backgroundView = UIView()
        backgroundView.backgroundColor = .black
        backgroundView.alpha = 0
        return backgroundView
    }()
    
    private let alertView: UIView = {
        let alertView = UIView()
        alertView.backgroundColor = .white
        alertView.layer.masksToBounds = true
        alertView.layer.cornerRadius = 12
        return alertView
    }()
    
    func showAlert(with title: String,
                   message: String,
                   on viewController: UIViewController) {
        guard let targetView = viewController.view else {
            return
        }
        
        
        backgroundView.frame = targetView.bounds
        
        targetView.addSubview(backgroundView)
        targetView.addSubview(alertView)
        
        alertView.frame.size.width = targetView.frame.size.width-30
        alertView.frame.size.height = targetView.frame.size.height/5
        
        alertView.center = targetView.center
        
        let titleLabel = UILabel(frame: CGRect(x: alertView.frame.size.width*0.25,
                                               y: alertView.frame.size.height*0.1,
                                               width: alertView.frame.size.width*0.5,
                                               height: alertView.frame.size.height*0.2))
        titleLabel.text = title // parameter
        titleLabel.textAlignment = .center
        titleLabel.textColor = .black
        titleLabel.font = UIFont(name: "Roboto-Bold", size: 20)
        
        alertView.addSubview(titleLabel)
        
        let messageLabel = UILabel(frame: CGRect(x: alertView.frame.size.width*0.03,
                                                 y: alertView.frame.size.height*0.3,
                                                 width: alertView.frame.size.width-20,
                                                 height: alertView.frame.size.height*0.2))
        messageLabel.text = message
        messageLabel.numberOfLines = 1
        messageLabel.textAlignment = .center
        messageLabel.textColor = .black
        messageLabel.font = UIFont(name: "Roboto-Regular", size: 16)
        
        alertView.addSubview(messageLabel)
        
        let button = UIButton(frame: CGRect(x: alertView.frame.size.width*0.05,
                                            y: alertView.frame.size.height*0.6,
                                            width: alertView.frame.size.width*0.9,
                                            height: 50))
        button.setTitle("OK", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.backgroundColor = .systemGreen
        button.layer.cornerRadius = 25
        button.layer.masksToBounds = true
        button.titleLabel?.font = UIFont(name: "Roboto-Bold", size: 20)
        button.addTarget(self, action: #selector(dismissAlert), for: .touchUpInside)
        
        alertView.addSubview(button)
        
        UIView.animate(withDuration: 0.25,
                       animations: {
            self.backgroundView.alpha = 0.5
        }, completion: { done in
            if done {
                UIView.animate(withDuration: 0.25,animations: {
                    self.alertView.center = targetView.center
                    return
                })
            }
        })
    }
    
    @objc func dismissAlert() {
        UIView.animate(withDuration: 0.25, animations: {
            self.backgroundView.alpha = 0
        }, completion: { done in
            if done {
                
                self.alertView.removeFromSuperview()
                self.backgroundView.removeFromSuperview()
            }
        })
    }
} '''

[ad_2]

Source link

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments