Skip to content

Instantly share code, notes, and snippets.

@bsmith11
Created September 8, 2016 05:50
Show Gist options
  • Save bsmith11/d3c3ce153f48ffb2972cf8512534a92b to your computer and use it in GitHub Desktop.
Save bsmith11/d3c3ce153f48ffb2972cf8512534a92b to your computer and use it in GitHub Desktop.
import UIKit
import Anchorage
protocol MessageDisplayable {
func configureMessageView(topLayoutGuide: UILayoutSupport)
func displayMessage(message: String, animated: Bool)
func hideMessageAnimated(animated: Bool)
}
extension MessageDisplayable where Self: UIViewController {
var messageLayoutGuide: UILayoutSupport {
return messageView
}
func configureMessageView(topLayoutGuide: UILayoutSupport) {
view.addSubview(messageView)
messageView.topAnchor == topLayoutGuide.bottomAnchor
messageView.leadingAnchor == view.leadingAnchor
messageView.trailingAnchor == view.trailingAnchor
}
func displayMessage(message: String, animated: Bool) {
messageView.configureWithTitle(message)
messageView.display(true, animated: animated)
}
func hideMessageAnimated(animated: Bool) {
messageView.display(false, animated: animated)
}
}
// MARK: - Private
private extension MessageDisplayable where Self: UIViewController {
var messageView: MessageView {
guard let object = objc_getAssociatedObject(self, &MessageView.associatedKey) as? MessageView else {
let object = MessageView(frame: .zero)
objc_setAssociatedObject(self, &MessageView.associatedKey, object, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return object
}
return object
}
}
import UIKit
import Anchorage
class MessageView: UIView {
private let titleLabel = UILabel(frame: .zero)
private var heightConstraint: NSLayoutConstraint?
static var associatedKey = "com.bradsmith.messageViewAssociatedKey"
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.messageGreenColor()
configureViews()
configureLayout()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// MARK: - Public
extension MessageView {
func configureWithTitle(title: String) {
titleLabel.text = title
}
func display(display: Bool, animated: Bool) {
let animations = {
self.heightConstraint?.active = !display
self.superview?.layoutIfNeeded()
}
if animated {
UIView.animateWithDuration(0.3, animations: animations)
}
else {
animations()
}
}
}
// MARK: - Private
private extension MessageView {
func configureViews() {
titleLabel.font = UIFont.systemFontOfSize(16.0, weight: UIFontWeightLight)
titleLabel.textColor = UIColor.whiteColor()
titleLabel.numberOfLines = 0
titleLabel.lineBreakMode = .ByWordWrapping
titleLabel.textAlignment = .Center
addSubview(titleLabel)
}
func configureLayout() {
heightConstraint = heightAnchor == 0.0
titleLabel.verticalAnchors == verticalAnchors + 10.0 ~ UILayoutPriorityDefaultLow
titleLabel.horizontalAnchors == horizontalAnchors + 10.0 ~ UILayoutPriorityDefaultHigh - 1.0
}
}
// MARK: - UILayoutSupport
extension MessageView: UILayoutSupport {
var length: CGFloat {
return bounds.height
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment