Is there any way to Toast message in swift ?
I have tried in objective c but could not find solution in swift.
[self.view makeToast:@\"Account create
if makeToast:duration:position:
is defined in objective-c and can be called, then the swift code will be
self.view.makeToast("Acount created Successfully", duration: 0.5, position: "bottom")
You may need to use a bridging header to gain access to those method in your swift code though.
This will help for you it will make the toast at the center with proper padding
func showToast(message:String,view:UIView){
let toastLabel = PaddingLabel()
toastLabel.frame = CGRect(x:0, y: view.frame.size.height-100, width: view.frame.width-50, height: 0)
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = .center;
toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
toastLabel.text = message
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
toastLabel.sizeToFit()
toastLabel.frame.origin.x=(view.frame.width/2)-(toastLabel.frame.width/2)
view.addSubview(toastLabel)
UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
}
And add this PaddingLabel file for label padding
import Foundation
import UIKit
class PaddingLabel: UILabel {
let padding=UIEdgeInsetsMake(5, 10, 5,10)
override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, padding))
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
let superSizeThatFits=super.sizeThatFits(size)
let width=superSizeThatFits.width+padding.left+padding.right
let height=superSizeThatFits.height+padding.top+padding.bottom
return CGSize(width: width, height: height)
}
}
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:
Instead of using UILabel
using UITextView
gets better results.
func showToast(message: String) {
let toastLabel = UITextView(frame: CGRect(x: self.view.frame.size.width/16, y: self.view.frame.size.height-150, width: self.view.frame.size.width * 7/8, height: 35))
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = .center;
toastLabel.text = " \(message) "
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
toastLabel.font = UIFont(name: (toastLabel.font?.fontName)!, size: 16)
toastLabel.layoutEdgeInsets.left = 8
toastLabel.layoutEdgeInsets.right = 8
toastLabel.center.x = self.view.frame.size.width/2
self.view.addSubview(toastLabel)
UIView.animate(withDuration: 5.0, delay: 0.1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
}
Space is added with message to provide good spacing at the both ends so that it looks good. Modified version of answer of Mr.Bean
static func popUp(context ctx: UIViewController, msg: String) {
let toast = UILabel(frame:
CGRect(x: 16, y: ctx.view.frame.size.height / 2,
width: ctx.view.frame.size.width - 32, height: 100))
toast.backgroundColor = UIColor.lightGray
toast.textColor = UIColor.white
toast.textAlignment = .center;
toast.numberOfLines = 3
toast.font = UIFont.systemFont(ofSize: 20)
toast.layer.cornerRadius = 12;
toast.clipsToBounds = true
toast.text = msg
ctx.view.addSubview(toast)
UIView.animate(withDuration: 5.0, delay: 0.2,
options: .curveEaseOut, animations: {
toast.alpha = 0.0
}, completion: {(isCompleted) in
toast.removeFromSuperview()
})
}
Then just call it from UIViewController
popUp(context: self, msg: "Your message")
@mr-bean code updated to latest Swift version (3.x)
let toastLabel =
UILabel(frame:
CGRect(x: self.view.frame.size.width/2 - 150,
y: self.view.frame.size.height-100,
width: 300,
height: 35))
toastLabel.backgroundColor = UIColor.black
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = NSTextAlignment.center
self.view.addSubview(toastLabel)
toastLabel.text = message
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
UIView.animate(withDuration: 4.0, animations: {
toastLabel.alpha = 0.0
})