Member-only story
Swift UIKit Cheat Sheet: The Ultimate Guide to Creating Views with Code
The Fastest Way to Learn UIKit Views with Code
So, you’ve decided to dive into UIKit and create views programmatically? Congratulations! You’ve officially entered the club of developers who say, “No thanks” to Storyboards and “Yes please” to code. This cheat sheet is here to help you wield the power of UIView
like a true wizard.
By the time you finish this, you’ll have all the UIKit tricks up your sleeve and be ready to take on the world of iOS development like a boss. Let’s get started!
This article is part of my UIKit Programmatic Cheat Sheet series. This tutorial assumes you already have basic Swift UIKit knowledge, such as understanding how Storyboard and ViewControllers work.
Creating and Adding a UIView
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myView = UIView()
myView.backgroundColor = .systemBlue
myView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(myView)
NSLayoutConstraint.activate([
myView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
myView.centerYAnchor.constraint(equalTo…