Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a toast message in Swift?

Tags:

ios

swift

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"]; 
like image 532
Aabid Khan Avatar asked Jul 21 '15 13:07

Aabid Khan


People also ask

How do I show toast in Swift UI?

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.

Does iOS have toast message?

A toast message in iOS is a small, short-lived popup that provides a small bite of information to the users.

What is the proper format for creating a toast message?

Creating a Toast Toast toast = Toast. makeText(getApplicationContext(), "This is a message displayed in a Toast", Toast. LENGTH_SHORT); toast. show();


2 Answers

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)) 
like image 83
Mr. Bean Avatar answered Oct 14 '22 09:10

Mr. Bean


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()             })         })     } } 
like image 32
Samo Avatar answered Oct 14 '22 09:10

Samo