There is a 3rd party library that supports customizable toast notification with single line of code. Here is a simple example of it:
import Toast_Swift
...
// basic usage
self.view.makeToast("This is a piece of toast")
// toast with a specific duration and position
self.view.makeToast("This is a piece of toast", duration: 3.0, position: .top)
I know there are accepted answers but they all seem to have a big flaw - if you show several toasts in a short period of time they will show on top of each other. Here is my implementation which takes this problem into consideration:
class Toast: UILabel {
private let BOTTOM_MARGIN: CGFloat = 16
private let SIDE_MARGIN: CGFloat = 16
private let HEIGHT: CGFloat = 35
private let SHOW_TIME_SECONDS = TimeInterval(3)
private let BACKGROUND_COLOR = UIColor.darkGray.withAlphaComponent(0.7).cgColor
private let TEXT_COLOR = UIColor.white
private let ANIMATION_DURATION_SEC = 0.33
private static var queue: [ToastHolder] = []
private static var showing: Toast?
init(_ text: String) {
super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
self.text = text
self.textColor = TEXT_COLOR
textAlignment = .center
self.layer.backgroundColor = BACKGROUND_COLOR
self.layer.cornerRadius = 5
}
public func show(_ parent: UIViewController) {
frame = CGRect(x: SIDE_MARGIN, y: UIScreen.main.bounds.height - BOTTOM_MARGIN - HEIGHT, width: UIScreen.main.bounds.width - 2 * SIDE_MARGIN, height: HEIGHT)
if Toast.showing == nil {
Log.d("showing \(String(describing: text))")
Toast.showing = self
alpha = 0
parent.view.addSubview(self)
UIView.animate(withDuration: ANIMATION_DURATION_SEC, animations: {
self.alpha = 1
}, completion: { (completed) in
Timer.scheduledTimer(timeInterval: self.SHOW_TIME_SECONDS, target: self, selector: #selector(self.onTimeout), userInfo: nil, repeats: false)
})
} else {
Toast.queue.append(ToastHolder(self, parent))
}
}
@objc func onTimeout() {
UIView.animate(withDuration: ANIMATION_DURATION_SEC, animations: {
self.alpha = 0
}, completion: { (completed) in
Toast.showing = nil
self.removeFromSuperview()
if !Toast.queue.isEmpty {
let holder = Toast.queue.removeFirst()
holder.toast.show(holder.parent)
}
})
}
required init?(coder aDecoder: NSCoder) {
fatalError("this initializer is not supported")
}
private class ToastHolder {
let toast: Toast
let parent: UIViewController
init(_ t: Toast, _ p: UIViewController) {
toast = t
parent = p
}
}
}
I have been using this extension when ever i need toast-message like android..
Just copy the extension to you project and then in your UIViewController class, call the function like
If the need is for a simple Toast message without fancy customization of font, alignment, text color, etc. then the following would do just fine
let messageVC = UIAlertController(title: "Message Title", message: "Account Created successfully" , preferredStyle: .actionSheet)
present(messageVC, animated: true) {
Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false, block: { (_) in
messageVC.dismiss(animated: true, completion: nil)})}
.actionSheet presents the alert from Bottom of the screen and the Timer takes care of the display duration. You can add this as an extension to UIViewController and then call it from anywhere.
Let me add this to this chain of answers: This library does what you need DCToastView allowing you to provide toast messages from the top or bottom side of the screen:
You will just need to add the pod
pod 'DCToastView'
Import it where you want to use it.
import DCToastView
And use it
ToastPresenter.shared.show(in: self.view, message: "This is a toast")
You can pass the following properties to the show method:
view: The view in which the toast is going to be presented
message: The message that the toast will show
toastPlace: The place which can be .down or .up
backgroundColor: The background color of the toast; defaults to black
textColor: The text color of the message; defaults to white
timeOut: The amount of seconds for the toast to dismiss if not provided it means that the toast will be sticky (will remain until touched); defaults to nil
roundness: How round the toast will be: .none, .low, .mid, .high; defaults to .mid
The best and easiest way to add a toast on an iOS app is using this library called Loafjet. The library is really small(27kb), won't make much difference to your project. Try it!