Is there any way to make a toast message in swift ?
I have tried in objective c but could not find solution in swift.
[self.view makeToast:@"Account created Successfully" duration:0.5 position:@"bottom"];
ToastUI is available through Swift Package Manager. For app integration, add ToastUI to an existing Xcode project as a package dependency: From the File menu, select Swift Packages > Add Package Dependency... Enter https://github.com/quanshousio/ToastUI into the package repository URL text field.
A toast message in iOS is a small, short-lived popup that provides a small bite of information to the users.
Creating a Toast Toast toast = Toast. makeText(getApplicationContext(), "This is a message displayed in a Toast", Toast. LENGTH_SHORT); toast. show();
extension UIViewController { func showToast(message : String, font: UIFont) { let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 75, y: self.view.frame.size.height-100, width: 150, height: 35)) toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6) toastLabel.textColor = UIColor.white toastLabel.font = font toastLabel.textAlignment = .center; toastLabel.text = message toastLabel.alpha = 1.0 toastLabel.layer.cornerRadius = 10; toastLabel.clipsToBounds = true self.view.addSubview(toastLabel) UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: { toastLabel.alpha = 0.0 }, completion: {(isCompleted) in toastLabel.removeFromSuperview() }) } }
Use like this:
self.showToast(message: "Your Toast Message", font: .systemFont(ofSize: 12.0))
For Swift 4
My version of a Toast that uses layout constraints, with the advantage that it works for any text size, as is (based in the response of Tony Franzis):
Just call: Toast.show(message: "My message", myViewControllerName)
class Toast { static func show(message: String, controller: UIViewController) { let toastContainer = UIView(frame: CGRect()) toastContainer.backgroundColor = UIColor.black.withAlphaComponent(0.6) toastContainer.alpha = 0.0 toastContainer.layer.cornerRadius = 25; toastContainer.clipsToBounds = true let toastLabel = UILabel(frame: CGRect()) toastLabel.textColor = UIColor.white toastLabel.textAlignment = .center; toastLabel.font.withSize(12.0) toastLabel.text = message toastLabel.clipsToBounds = true toastLabel.numberOfLines = 0 toastContainer.addSubview(toastLabel) controller.view.addSubview(toastContainer) toastLabel.translatesAutoresizingMaskIntoConstraints = false toastContainer.translatesAutoresizingMaskIntoConstraints = false let a1 = NSLayoutConstraint(item: toastLabel, attribute: .leading, relatedBy: .equal, toItem: toastContainer, attribute: .leading, multiplier: 1, constant: 15) let a2 = NSLayoutConstraint(item: toastLabel, attribute: .trailing, relatedBy: .equal, toItem: toastContainer, attribute: .trailing, multiplier: 1, constant: -15) let a3 = NSLayoutConstraint(item: toastLabel, attribute: .bottom, relatedBy: .equal, toItem: toastContainer, attribute: .bottom, multiplier: 1, constant: -15) let a4 = NSLayoutConstraint(item: toastLabel, attribute: .top, relatedBy: .equal, toItem: toastContainer, attribute: .top, multiplier: 1, constant: 15) toastContainer.addConstraints([a1, a2, a3, a4]) let c1 = NSLayoutConstraint(item: toastContainer, attribute: .leading, relatedBy: .equal, toItem: controller.view, attribute: .leading, multiplier: 1, constant: 65) let c2 = NSLayoutConstraint(item: toastContainer, attribute: .trailing, relatedBy: .equal, toItem: controller.view, attribute: .trailing, multiplier: 1, constant: -65) let c3 = NSLayoutConstraint(item: toastContainer, attribute: .bottom, relatedBy: .equal, toItem: controller.view, attribute: .bottom, multiplier: 1, constant: -75) controller.view.addConstraints([c1, c2, c3]) UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseIn, animations: { toastContainer.alpha = 1.0 }, completion: { _ in UIView.animate(withDuration: 0.5, delay: 1.5, options: .curveEaseOut, animations: { toastContainer.alpha = 0.0 }, completion: {_ in toastContainer.removeFromSuperview() }) }) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With