Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
188: Auto Layout Programmatically
Why are my constraints being ignored or causing conflicts the moment I run the app?
This is the "rite of passage" for everyone learning programmatic layout. You spend twenty minutes perfectly defining your anchors, run the app, and your view is either stuck at (0,0) with a size of 0x0, or your console is screaming about conflicting constraints.
The culprit is almost always translatesAutoresizingMaskIntoConstraints. By default, views created in code have this property set to true. This is a leftover from the old days of "springs and struts" (Autoresizing Masks). When it's true, iOS automatically creates a set of constraints that mimic the old system. If you then add your own Auto Layout constraints, they clash with those auto-generated ones.
I've lost count of how many times I forgot this. Just remember: if you're doing Auto Layout in code, you must set this to false before adding the view to the hierarchy.
let profileImageView = UIImageView()
profileImageView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(profileImageView)
What's the cleanest way to actually define the constraints?
You might see some older tutorials using NSLayoutConstraint(item:attribute:relatedBy:toItem:attribute:multiplier:constant:). Please, for your own sanity, ignore that. It's verbose, hard to read, and a nightmare to debug.
Use Layout Anchors. They make the code read much more like a sentence. Let's say we're building a simple profile header where we want an image centered in the screen and a name label pinned right below it. It looks like this:
// Center the image NSLayoutConstraint.activate([ profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor), profileImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor), profileImageView.widthAnchor.constraint(equalToConstant: 100), profileImageView.heightAnchor.constraint(equalToConstant: 100) ]) // Pin the label to the bottom of the image NSLayoutConstraint.activate([ nameLabel.topAnchor.constraint(equalTo: profileImageView.bottomAnchor, constant: 16), nameLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor) ])Notice how
topAnchor.constraint(equalTo: ...)clearly describes the relationship. It's intuitive, and the compiler will catch it if you try to constrain atopAnchor(Y-axis) to aleadingAnchor(X-axis).Do I really need to call activate() on every single constraint?
You could call
.isActive = trueon every single line, but that's tedious and actually slightly worse for performance. Every time you activate a constraint individually, the system potentially triggers a layout pass to re-calculate the entire UI.The professional way to do it is using
NSLayoutConstraint.activate([]). This method takes an array of constraints and activates them all in one single batch. It's cleaner, faster, and keeps your layout logic grouped together. I usually group my constraints by the view they belong to, so I can easily find where the "image layout" ends and the "label layout" begins.
📋 Practical Task
Build a Centered Profile Card
Create a UIView subclass called ProfileCardView. Inside this view, programmatically add an UIImageView and a UILabel. Your goal is to achieve the following layout using Auto Layout anchors:
- The
UIImageViewshould be a 60x60 square, centered horizontally in the card, and pinned to the top of the card with a 20pt margin. - The
UILabelshould be centered horizontally, pinned to the bottom of the image with a 10pt margin, and pinned to the bottom of the card with a 20pt margin. - Ensure all views have
translatesAutoresizingMaskIntoConstraintsset tofalse.
Test your implementation by adding the ProfileCardView to a View Controller and giving the card a background color so you can see the constraints working.
There are no comments for now.