Categories: iOSSwift Programming

Android like Toast in iOS

Hello everyone!

Today I’m going to show you how you can easily create toast in iOS. To achieve this task I’m going to use Charles Scalesse’s Toast API.

First of all download the file from the above link, then add UIView+Toast.h and UIView+Toast.m to your project along with the QuartzCore framework.

As soon as you’ll try adding UIView+Toast.h and UIView+Toast.m to your project a prompt will appear asking to add a bridging-header. Press “YES”. A file ending with  -Bridging-Header.h will be available in your project explorer. Open it and write #import “UIView+Toast.h”.

Now lets say you have a button on which on press you want to make a toast for that purpose write the following code in your ViewController.swift or what ever your class name is.

[code language=”css”] @IBAction func simpleToast(sender: AnyObject) {
self.view.makeToast(“I Love Swift”);
} [/code]

You’re gonna get a result similar to this:

If you want a toast to appear in the center and with a title.

[code language=”css”] @IBAction func positionalToast(sender: UIButton){
self.view.makeToast(“I Love Swift”, duration: 5.0, position: CSToastPositionCenter, title: “Hello!”)

}
[/code]

This is what you’ll see.

A toast with an image is also possible with this API.

@IBAction func imageToast(sender: AnyObject) {
self.view.makeToast("I Love Swift", duration: 5.0, position: CSToastPositionCenter, image: UIImage(named: "swift.png"))

}

Resulting toast.

I have made this project, to save time you can download or pull it from my Github.

If you guys have any questions, please leave a comment.

Aaqib Hussain

Recent Posts

Things to know when moving to Germany

This article covers some important things you must know when you are considering a move…

3 years ago

Unit Testing in Android for Dummies

What is Unit Testing? In its simplest term, unit testing is testing a small piece…

4 years ago

Factory Design Pattern

In this article, you will learn about a type of Creational Design Pattern which is…

5 years ago

Creating Target specific Theme in iOS

In this tutorial, you will go through the use of targets to achieve two separate…

5 years ago

Facade Design Pattern

In this article, you will learn about a type of Structural Design Pattern which is…

5 years ago

Singleton Design Pattern

In this article you will learn about a type of Creational Design Pattern which is…

5 years ago