Create UIKit view controllers with Auto Layout
✓Works with OpenClaudeYou are a Swift iOS developer specializing in UIKit. The user wants to create view controllers with properly configured Auto Layout constraints using UIKit.
What to check first
- Verify your project uses UIKit (not SwiftUI) by checking the
AppDelegate.swiftfile - Confirm your view controller has
import UIKitat the top - Check that
translatesAutoresizingMaskIntoConstraintsis set tofalseon all views you're constraining
Steps
- Create a new
UIViewControllersubclass and overrideviewDidLoad() - Instantiate your UI elements (UILabel, UIButton, UIImageView, etc.) and set
translatesAutoresizingMaskIntoConstraints = false - Add subviews to
self.viewusingaddSubview() - Create constraint objects using
NSLayoutConstraintwithconstant,multiplier,relation, andpriorityparameters - Activate constraints by calling
NSLayoutConstraint.activate([constraint1, constraint2])or setisActive = trueon each - Use
safeAreaLayoutGuidefor top/bottom constraints to account for notches and home indicators - Test on multiple device sizes in the Simulator to verify responsive layout
- Use
UIStackViewfor common layouts (rows/columns) to reduce constraint boilerplate
Code
import UIKit
class AutoLayoutViewController: UIViewController {
private let titleLabel = UILabel()
private let descriptionLabel = UILabel()
private let actionButton = UIButton(type: .system)
private let containerView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
setupSubviews()
setupConstraints()
}
private func setupSubviews() {
// Configure title label
titleLabel.text = "Welcome"
titleLabel.font = UIFont.systemFont(ofSize: 24, weight: .bold)
titleLabel.textAlignment = .center
titleLabel.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(titleLabel)
// Configure description label
descriptionLabel.text = "This is an Auto Layout example"
descriptionLabel.font = UIFont.systemFont(ofSize: 16)
descriptionLabel.numberOfLines = 0
descriptionLabel.textAlignment = .center
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(descriptionLabel)
// Configure button
actionButton.setTitle("Tap Me", for: .normal)
actionButton.backgroundColor = .systemBlue
actionButton.setTitleColor(.white, for: .normal)
actionButton.layer.cornerRadius = 8
actionButton.translatesAutoresizingMaskIntoConstraints
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Swift / iOS Skills
Other Claude Code skills in the same category — free to download.
SwiftUI
Build SwiftUI views with state management and navigation
Core Data
Set up Core Data with models, contexts, and fetch requests
Swift Networking
Build networking layer with URLSession and Codable
Swift Combine
Use Combine for reactive programming in Swift
Swift Testing
Write XCTest unit and UI tests for iOS apps
Swift Async/Await Concurrency
Use Swift's structured concurrency for async code without callback hell
Swift CoreData Relationships
Model one-to-many and many-to-many relationships in CoreData
Want a Swift / iOS skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.