Skip to content

Instantly share code, notes, and snippets.

@amosavian
Created September 4, 2016 10:17
Show Gist options
  • Save amosavian/44f2fca3d1112f9e33f94fa14f991d4c to your computer and use it in GitHub Desktop.
Save amosavian/44f2fca3d1112f9e33f94fa14f991d4c to your computer and use it in GitHub Desktop.
static func showCommentOnView(view: UIView, comment: String = "", topPadding: CGFloat = 0) {
if let view = view as? UITableView {
view.backgroundView = nil
} else if let view = view as? UICollectionView {
view.backgroundView = nil
}
while let commentView = view.viewWithTag(1001) {
commentView.removeFromSuperview();
}
if !comment.isEmpty {
// Comment View
let commentView = getCommentView(comment)
if let view = view as? UITableView {
view.backgroundView = commentView
} else if let view = view as? UICollectionView {
view.backgroundView = commentView
} else {
view.addSubview(commentView);
view.bringSubviewToFront(commentView)
commentView.frame = view.bounds
}
commentView.translatesAutoresizingMaskIntoConstraints = true
commentView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
}
}
static func getCommentView(comment: String) -> UIView {
let commentView = UIView();
commentView.tag = 1001;
// Comment Label
let commentLabel = UILabel();
commentLabel.text = comment
commentLabel.textAlignment = NSTextAlignment.Center;
commentLabel.textColor = UIColor.grayColor();
commentLabel.numberOfLines = 0;
commentLabel.lineBreakMode = .ByWordWrapping
commentLabel.tag = 1001;
commentLabel.translatesAutoresizingMaskIntoConstraints = false
commentView.addSubview(commentLabel);
commentView.addConstraint(NSLayoutConstraint(item: commentLabel, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: commentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 20))
commentView.addConstraint(NSLayoutConstraint(item: commentLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: commentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
commentView.addConstraint(NSLayoutConstraint(item: commentLabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: commentView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
commentView.addConstraint(NSLayoutConstraint(item: commentLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: commentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 20))
return commentView
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment