Updated 28 October 2024
In this article you will learn about how To Make Your Own Toast View In Swift in Swift.
A Toast View in Swift is a small, temporary message that appears on the screen to give feedback to users without interrupting what they are doing.
It’s often used to show short notifications like confirmations or status updates.
You can create your own toast message displaying UIView in swift , it is a great way to add user-friendly notifications to your app.
We need to follow below mention steps.
First of all we need to create a new project where you will Make Your Own Toast View.
After that we need to add a UIButton either programatically or using Storyboard.
1 |
@IBOutlet weak var toastButton: UIButton! |
You can check here for more knowledge about UIButton.
UIButton is a control element that allows users to interact with your app by tapping it. It can display text, images, or both, and can trigger actions when pressed.
In this step we will create our own toastView.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class GlobalData{ class func toastView(messsage : String, view: UIView ){ let toastLabel = UILabel(frame: CGRect(x: view.frame.size.width/2 - 150, y: view.frame.size.height-100, width: 300, height : 35)) toastLabel.backgroundColor = UIColor.green.withAlphaComponent(0.6) toastLabel.textColor = UIColor.black toastLabel.textAlignment = NSTextAlignment.center; view.addSubview(toastLabel) toastLabel.text = messsage toastLabel.alpha = 1.0 toastLabel.layer.cornerRadius = 10; toastLabel.clipsToBounds = true UIView.animate(withDuration: 4.0, delay: 0.3, options: UIView.AnimationOptions.curveEaseOut, animations: { toastLabel.alpha = 0.0 }) } } |
In above code we have create a our own toastView.
In this step we will call the toastView on click on button action.
1 2 3 |
@IBAction func toastButtonAction(_ sender: Any) { GlobalData.toastView(messsage: "Your Message is Display here", view: self.view) } |
In above code an action method that is triggered when a button is tapped.
We need to call toastView method from toastButtonAction.
A Toast View in Swift is a small, temporary message that appears on the screen to give feedback to users without interrupting what they are doing
In this article we have discussed about how to Make Your Own Toast View In Swift.
I hope this blog is helpful to understand this topic.
Thanks for reading this blog. You can also check other blogs from here for more knowledge.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.