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
You can create your own style of toast and align it to the center of the whole screen like this:
func showToast(message: String, in viewController: UIViewController) {
let toastContainer: UIView = {
let view = UIView(frame: CGRect())
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.gray
view.alpha = 0.0
view.layer.cornerRadius = 12
view.clipsToBounds = true
return view
}()
let toastLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = message
label.textColor = UIColor.white
label.textAlignment = .center
label.font = label.font.withSize(20)
label.clipsToBounds = true
label.numberOfLines = 0
return label
}()
toastContainer.addSubview(toastLabel)
viewController.view.addSubview(toastContainer)
toastContainer.layer.zPosition = 1
NSLayoutConstraint.activate([
toastContainer.widthAnchor.constraint(equalToConstant: viewController.view.frame.width * 0.6),
toastContainer.heightAnchor.constraint(equalToConstant: viewController.view.frame.height * 0.1),
toastContainer.centerXAnchor.constraint(equalTo: viewController.view.centerXAnchor),
toastContainer.bottomAnchor.constraint(equalTo: viewController.view.bottomAnchor, constant:
-UIScreen.main.bounds.maxY / 2 + viewController.view.frame.height * 0.1 / 2
),
toastLabel.centerXAnchor.constraint(equalTo: toastContainer.centerXAnchor),
toastLabel.centerYAnchor.constraint(equalTo: toastContainer.centerYAnchor)
])
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseIn, animations: {
toastContainer.alpha = 1.0
}) { (_) in
UIView.animate(withDuration: 0.5, delay: 2, options: .curveEaseOut, animations: {
toastContainer.alpha = 0.0
}) { (_) in
toastContainer.removeFromSuperview()
}
}
}