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
210: Right-to-Left Layout Support
Most of us develop our apps in English, so we get used to the "left-to-right" (LTR) flow. But if you're targeting a global audience, you can't just translate the strings and call it a day. In languages like Arabic or Hebrew, the entire mental model of the UI flips. If you've hardcoded "left" and "right" anywhere in your layout, your app is going to look broken the moment a user switches their system language.
Building a Direction-Aware Profile Row
I want to build a simple user profile row. It’ll have an avatar on one side, a name and email in the middle, and a "Verified" badge on the other. To make this work across both LTR and RTL, the golden rule is to stop thinking in terms of left and right and start thinking in leading and trailing.
In SwiftUI, leading means "where the text starts" and trailing means "where it ends." In English, leading is left. In Arabic, leading is right. Let's see how that looks in code:
struct ProfileRow: View {
var body: some View {
HStack(spacing: 16) {
Image(systemName: "person.circle.fill")
.resizable()
.frame(width: 50, height: 50)
VStack(alignment: .leading) {
Text("Amira Mansour")
.font(.headline)
Text("amira@example.com")
.font(.subheadline)
.foregroundColor(.secondary)
}
Spacer()
Image(systemName: "checkmark.seal.fill")
.foregroundColor(.blue)
}
.padding()
}
}
Notice I used alignment: .leading in the VStack. Because I didn't specify .left, SwiftUI will automatically flip that alignment to the right when the app is run in an RTL locale. The HStack also handles the order of elements automatically. The avatar will be on the left in English and on the right in Arabic without me changing a single line of code.
The Offset Trap
Here is where I usually mess up. Let's say I want to add a little visual flair—maybe I want the verified badge to slightly overlap the edge of the row. My instinct is to just nudge it over with an offset.
I'll add this to the badge image:
.offset(x: 10)
When I test this in English, it looks great. It pushes the badge 10 points to the right. But when I switch the simulator to Arabic, the layout flips, but the offset doesn't. The badge is now being pushed 10 points to the right, which in an RTL layout actually pushes it further away from the center, potentially clipping it off the screen or creating a weird gap. I've just created a bug that only exists for a fraction of my users.
Correcting Directional Shifts
To fix this, we can't use a raw number for the X-offset if that offset is intended to be "towards the end of the line." We need to make the offset value dynamic based on the current layout direction.
I can use the @Environment(\.layoutMode) property, but a cleaner way to handle "directional" offsets in a simple row is to avoid .offset entirely and use padding or a negative spacer. However, if you absolutely need a coordinate shift, you have to check the direction manually:
@Environment(\.layoutDirection) var direction
// ... inside the view ...
.offset(x: direction == .rightToLeft ? -10 : 10)
By checking layoutDirection, I'm explicitly telling the app: "If we are in RTL mode, move the element in the opposite direction." It's a bit more verbose, but it's the only way to ensure your "flair" doesn't become a "fail" in different locales. Now, whether the user is in New York or Cairo, the badge sits exactly where it should relative to the edge of the screen.
📋 Practical Task
The RTL-Compliant Settings Toggle
Build a "Settings Toggle" component that consists of a label on the leading side, a toggle switch on the trailing side, and a small "info" icon (info.circle) positioned immediately to the left of the toggle.
Requirements:
- Use
HStackandSpacerto ensure the layout flips correctly between LTR and RTL. - The "info" icon must maintain its position relative to the toggle (it should always be "before" the toggle in the reading order).
- Add a custom
.offset(x: ...)to the info icon to move it slightly closer to the toggle, but implement a check using@Environment(\.layoutDirection)so the offset flips direction correctly in RTL mode.
There are no comments for now.