Skip to content

Commit

Permalink
Separate view creation in methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Emerson Mendes Filho committed May 23, 2021
1 parent d466007 commit 460a8a4
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions ViewCodeGuide/ViewCodeGuide/Source/MyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,45 @@ final class MyView: UIView {
super.init(frame: .zero)
self.backgroundColor = .orange

setupHierarchy()
setupConstraints()
}

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

lazy var titleLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.text = "My Title"
titleLabel.backgroundColor = .gray
titleLabel.translatesAutoresizingMaskIntoConstraints = false

addSubview(titleLabel)

titleLabel.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 24).isActive = true
titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16).isActive = true

return titleLabel
}()

lazy var subtitleLabel: UILabel = {
let subtitleLabel = UILabel()
subtitleLabel.text = "My Subtitle"
subtitleLabel.backgroundColor = .gray
subtitleLabel.translatesAutoresizingMaskIntoConstraints = false

return subtitleLabel
}()

func setupHierarchy() {
addSubview(titleLabel)
addSubview(subtitleLabel)

}

func setupConstraints() {
titleLabel.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 24).isActive = true
titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16).isActive = true

subtitleLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 16).isActive = true
subtitleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16).isActive = true
subtitleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16).isActive = true

}

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

0 comments on commit 460a8a4

Please sign in to comment.