Skip to content

Instantly share code, notes, and snippets.

@SashaTsebrii
Last active June 12, 2018 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SashaTsebrii/cff8f3ddc9ff3bc5b92d9f9396502903 to your computer and use it in GitHub Desktop.
Save SashaTsebrii/cff8f3ddc9ff3bc5b92d9f9396502903 to your computer and use it in GitHub Desktop.
Create label and set layout programmatically.
class ViewController: UIViewController {
// MARK: - Properties
let titleLabel: UILabel = {
let textLabel = UILabel()
textLabel.text = "Monday"
textLabel.font = UIFont.boldSystemFont(ofSize: 34)
textLabel.textAlignment = .center
return textLabel
}()
// MARK: - Lifecycle
override func loadView() {
super.loadView()
view.addSubview(titleLabel)
}
override func viewDidLoad() {
super.viewDidLoad()
setupLyout()
}
// MARK: - Private functions
private func setupLyout() {
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
titleLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: view.bounds.height * 0.1).isActive = true
titleLabel.widthAnchor.constraint(equalToConstant: view.bounds.width).isActive = true
titleLabel.heightAnchor.constraint(equalToConstant: view.bounds.height * 0.1).isActive = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment