Created
April 15, 2019 13:15
-
-
Save DanielCardonaRojas/d2961f752e9cef1344a0e512c922192c to your computer and use it in GitHub Desktop.
Collapsing expanding view
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
@IBDesignable | |
class RevealView: UIView { | |
var contentHeight: CGFloat = 300 | |
@IBInspectable var title: String = "" { | |
didSet { | |
titleLabel.text = title | |
} | |
} | |
@IBInspectable var titleColor: UIColor = .lightGray { | |
didSet { | |
titleLabel.textColor = titleColor | |
} | |
} | |
lazy var toggleExpandButton: UIButton = { | |
let button = UIButton(type: .custom) | |
button.tintColor = .black | |
button.setTitleColor(.black, for: .normal) | |
button.setTitle("Show less", for: .normal) | |
button.titleLabel?.font = UIFont.systemFont(ofSize: 14.0) | |
button.translatesAutoresizingMaskIntoConstraints = false | |
return button | |
}() | |
lazy var footerView: UIView = { | |
let view = UIView() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.backgroundColor = .white | |
view.clipsToBounds = true | |
return view | |
}() | |
lazy var contentView: UIView = { | |
let view = UIView() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.backgroundColor = .lightGray | |
return view | |
}() | |
lazy var titleLabel: UILabel = { | |
let label = UILabel() | |
label.textColor = .lightGray | |
label.font = UIFont.boldSystemFont(ofSize: 18.0) | |
label.numberOfLines = 1 | |
label.translatesAutoresizingMaskIntoConstraints = false | |
return label | |
}() | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setupViews() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
override func awakeFromNib() { | |
setupViews() | |
} | |
override func prepareForInterfaceBuilder() { | |
super.prepareForInterfaceBuilder() | |
toggleExpandButton.setTitle("View less", for: .normal) | |
} | |
func compressingConstant(for subview: UIView) -> CGFloat { | |
let footerHeight = footerView.frame.height | |
let origin = subview.alignmentRect(forFrame: subview.frame).origin | |
let originPoint = contentView.convert(origin, to: self) | |
return originPoint.y + footerHeight | |
} | |
private func setupViews() { | |
clipsToBounds = true | |
addSubview(titleLabel) | |
addSubview(contentView) | |
addSubview(footerView) | |
footerView.addSubview(toggleExpandButton) | |
NSLayoutConstraint.activate([ | |
footerView.bottomAnchor.constraint(equalTo: bottomAnchor), | |
footerView.rightAnchor.constraint(equalTo: rightAnchor), | |
footerView.leftAnchor.constraint(equalTo: leftAnchor), | |
footerView.heightAnchor.constraint(equalToConstant: 35), | |
]) | |
NSLayoutConstraint.activate([ | |
toggleExpandButton.heightAnchor.constraint(equalTo: footerView.heightAnchor), | |
toggleExpandButton.centerYAnchor.constraint(equalTo: footerView.centerYAnchor), | |
toggleExpandButton.rightAnchor.constraint(equalTo: footerView.rightAnchor, constant: -10.0), | |
]) | |
NSLayoutConstraint.activate([ | |
titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 16.0), | |
titleLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: 8.0), | |
titleLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: -8.0), | |
]) | |
NSLayoutConstraint.activate([ | |
contentView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 16.0), | |
contentView.rightAnchor.constraint(equalTo: rightAnchor, constant: -8.0), | |
contentView.leftAnchor.constraint(equalTo: leftAnchor, constant: 8.0), | |
contentView.heightAnchor.constraint(equalToConstant: contentHeight) | |
]) | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
bringSubviewToFront(toggleExpandButton) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment