Skip to content

Instantly share code, notes, and snippets.

@Cyanide7523
Last active August 30, 2018 01:02
Show Gist options
  • Save Cyanide7523/4bfd8e779b0434766fd5390ce295370c to your computer and use it in GitHub Desktop.
Save Cyanide7523/4bfd8e779b0434766fd5390ce295370c to your computer and use it in GitHub Desktop.
NotificationBar & NavigationBar Extension
/**
NotificationBar presents status of app's internal workflow visually.
In case of server disconnection... or receiving a new message... etc
*/
class NotificationBar: UIView{
private let messageLabel: UILabel = UILabel()
// You can change the mark as you wish, If it can be presented by Unicode text.
// If you want to present an image, try it your self.
private let checkMark = "✓ "
private let xMark = "✗ "
// Set this value as positive color.
let positiveColor = UIColor(red: 12/255, green: 206/255, blue: 55/255, alpha: 0.935)
// Set this value as negative color.
let negativeColor = UIColor(red: 206/255, green: 11/255, blue: 37/255, alpha: 0.935)
// If you set this, backgroundColor will be change refers to its value.
var isPositive: Bool = true{
didSet{
switch isPositive{
case true:
self.backgroundColor = positiveColor
case false:
self.backgroundColor = negativeColor
}
}
}
// If you set this, presenting text message will be changed.
var message: String?{
didSet{
if isPositive{
messageLabel.text = checkMark + (message ?? "")
}else{
messageLabel.text = xMark + (message ?? "")
}
}
}
override func draw(_ rect: CGRect) {
// Customize the label here.
messageLabel.frame = rect
messageLabel.font = UIFont.systemFont(ofSize: 13, weight: .light)
messageLabel.textColor = .white
messageLabel.textAlignment = .center
self.addSubview(messageLabel)
}
}
// - MARK : UINavigationBar Extension
// There are many ways of using NotificationBar, this is a sample of it.
extension UINavigationBar{
/**
Set this value to resize NotificationBar's presenting Size.
'32' is the best size to shown, but make your choice!
*/
private var defaultNotificationBarSize: CGFloat { return 32 }
/**
Presents notification bar with given message.
*/
func showNotification(message: String?, isPositive: Bool){
let bar = NotificationBar(frame: CGRect(x: self.frame.origin.x, y: self.frame.maxY - (defaultNotificationBarSize*1.6), width: self.frame.width, height: defaultNotificationBarSize))
bar.layer.zPosition = self.layer.zPosition - 1
bar.isPositive = isPositive
bar.message = message
self.addSubview(bar)
UIView.animate(withDuration: 0.5, delay: 0, options: [.curveEaseInOut], animations: {
bar.frame.origin.y += self.defaultNotificationBarSize
}) { _ in
UIView.animate(withDuration: 0.5, delay: 2, options: [.curveEaseInOut], animations: {
bar.frame.origin.y -= self.defaultNotificationBarSize
}, completion: { _ in
bar.removeFromSuperview()
})
}
}
}
@Cyanide7523
Copy link
Author

ezgif com-video-to-gif

Sample usage of NotificationBar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment