Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. @IBDesignable
  2. class RevealView: UIView {
  3.  
  4. var contentHeight: CGFloat = 300
  5.  
  6. @IBInspectable var title: String = "" {
  7. didSet {
  8. titleLabel.text = title
  9. }
  10. }
  11.  
  12. @IBInspectable var titleColor: UIColor = .lightGray {
  13. didSet {
  14. titleLabel.textColor = titleColor
  15. }
  16. }
  17.  
  18. lazy var toggleExpandButton: UIButton = {
  19. let button = UIButton(type: .custom)
  20. button.tintColor = .black
  21. button.setTitleColor(.black, for: .normal)
  22. button.setTitle("Show less", for: .normal)
  23. button.titleLabel?.font = UIFont.systemFont(ofSize: 14.0)
  24. button.translatesAutoresizingMaskIntoConstraints = false
  25. return button
  26. }()
  27.  
  28. lazy var footerView: UIView = {
  29. let view = UIView()
  30. view.translatesAutoresizingMaskIntoConstraints = false
  31. view.backgroundColor = .white
  32. view.clipsToBounds = true
  33. return view
  34. }()
  35.  
  36. lazy var contentView: UIView = {
  37. let view = UIView()
  38. view.translatesAutoresizingMaskIntoConstraints = false
  39. view.backgroundColor = .lightGray
  40. return view
  41. }()
  42.  
  43. lazy var titleLabel: UILabel = {
  44. let label = UILabel()
  45. label.textColor = .lightGray
  46. label.font = UIFont.boldSystemFont(ofSize: 18.0)
  47. label.numberOfLines = 1
  48. label.translatesAutoresizingMaskIntoConstraints = false
  49. return label
  50. }()
  51.  
  52. override init(frame: CGRect) {
  53. super.init(frame: frame)
  54. setupViews()
  55. }
  56.  
  57. required init?(coder aDecoder: NSCoder) {
  58. super.init(coder: aDecoder)
  59. }
  60.  
  61. override func awakeFromNib() {
  62. setupViews()
  63. }
  64.  
  65. override func prepareForInterfaceBuilder() {
  66. super.prepareForInterfaceBuilder()
  67. toggleExpandButton.setTitle("View less", for: .normal)
  68. }
  69.  
  70. func compressingConstant(for subview: UIView) -> CGFloat {
  71. let footerHeight = footerView.frame.height
  72. let origin = subview.alignmentRect(forFrame: subview.frame).origin
  73. let originPoint = contentView.convert(origin, to: self)
  74. return originPoint.y + footerHeight
  75. }
  76.  
  77. private func setupViews() {
  78. clipsToBounds = true
  79. addSubview(titleLabel)
  80. addSubview(contentView)
  81. addSubview(footerView)
  82. footerView.addSubview(toggleExpandButton)
  83.  
  84. NSLayoutConstraint.activate([
  85. footerView.bottomAnchor.constraint(equalTo: bottomAnchor),
  86. footerView.rightAnchor.constraint(equalTo: rightAnchor),
  87. footerView.leftAnchor.constraint(equalTo: leftAnchor),
  88. footerView.heightAnchor.constraint(equalToConstant: 35),
  89. ])
  90.  
  91. NSLayoutConstraint.activate([
  92. toggleExpandButton.heightAnchor.constraint(equalTo: footerView.heightAnchor),
  93. toggleExpandButton.centerYAnchor.constraint(equalTo: footerView.centerYAnchor),
  94. toggleExpandButton.rightAnchor.constraint(equalTo: footerView.rightAnchor, constant: -10.0),
  95. ])
  96.  
  97. NSLayoutConstraint.activate([
  98. titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 16.0),
  99. titleLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: 8.0),
  100. titleLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: -8.0),
  101. ])
  102.  
  103. NSLayoutConstraint.activate([
  104. contentView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 16.0),
  105. contentView.rightAnchor.constraint(equalTo: rightAnchor, constant: -8.0),
  106. contentView.leftAnchor.constraint(equalTo: leftAnchor, constant: 8.0),
  107. contentView.heightAnchor.constraint(equalToConstant: contentHeight)
  108. ])
  109. }
  110.  
  111. override func layoutSubviews() {
  112. super.layoutSubviews()
  113. bringSubviewToFront(toggleExpandButton)
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement