Usage
Toast.show(withMessage: "My message")
Result
import Foundation | |
import UIKit | |
class Toast { | |
static func show(withMessage message: String, withHeight height: CGFloat = 60.0, withDuration duration: TimeInterval = 3.0, withBackgroundColor backgroundColor: UIColor = UIColor.black, withTextColor textColor: UIColor = UIColor.white) { | |
let view = UIApplication.shared.keyWindow! | |
let toastLabel = UILabel(frame: CGRect(x: 0.0, y: -height, width: UIScreen.main.bounds.width, height: height)) | |
toastLabel.backgroundColor = backgroundColor | |
toastLabel.textColor = textColor | |
toastLabel.textAlignment = .center; | |
toastLabel.text = "\n\(message)" | |
toastLabel.font = toastLabel.font.withSize(12) | |
toastLabel.numberOfLines = 2 | |
toastLabel.alpha = 1.0 | |
toastLabel.clipsToBounds = true | |
view.addSubview(toastLabel) | |
UIView.animate(withDuration: 0.2, animations: { | |
toastLabel.frame.origin.y = 0 | |
}) { _ in | |
UIView.animate(withDuration: 0.2, delay: duration, animations: { | |
toastLabel.frame.origin.y = -height | |
}, completion: {(isCompleted) in | |
toastLabel.removeFromSuperview() | |
}) | |
} | |
} | |
} |