Created
March 31, 2019 19:25
-
-
Save Abhishek9634/fae32666a77b0a644c74c1f12f2ab5a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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