Last active
August 30, 2018 01:02
-
-
Save poca-p0ca/4bfd8e779b0434766fd5390ce295370c to your computer and use it in GitHub Desktop.
NotificationBar & NavigationBar Extension
This file contains hidden or 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
/** | |
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() | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample usage of NotificationBar