Skip to content

Instantly share code, notes, and snippets.

@Abhishek9634
Created March 31, 2019 19:25
Show Gist options
  • Save Abhishek9634/fae32666a77b0a644c74c1f12f2ab5a0 to your computer and use it in GitHub Desktop.
Save Abhishek9634/fae32666a77b0a644c74c1f12f2ab5a0 to your computer and use it in GitHub Desktop.
import UIKit
class SnackBar: UIView {
@IBOutlet weak var titleLabel: UILabel!
static var newInstance: SnackBar {
return Bundle.main.loadNibNamed("SnackBar",
owner: nil,
options: nil)!.first as! SnackBar
}
func configure(message: String) {
self.titleLabel.text = message
}
}
extension UIViewController {
func showSnackBar(msg: String, delay: TimeInterval = 0.6) {
guard let window = appDelegate.window else { return }
let bounds = window.bounds
let snackBar = SnackBar.newInstance
snackBar.configure(message: msg)
snackBar.frame = CGRect(x: 0.0,
y: 0.0,
width: bounds.width,
height: 64.0)
snackBar.alpha = 0
window.addSubview(snackBar)
window.bringSubview(toFront: snackBar)
UIView.animate(withDuration: 0.5, animations: {
snackBar.alpha = 1
}) { _ in
UIView.animate(withDuration: 0.5, delay: delay, animations: {
snackBar.alpha = 0
}) { _ in
snackBar.removeFromSuperview()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment