Mastering Swift UI- The Complete Guide (PART-2)

SwiftUI Views and Modifiers

Creating Reusable Views

import SwiftUI

struct CustomButton: View {
    var title: String
    var backgroundColor: Color
    
    var body: some View {
        Text(title)
            .font(.headline)
            .padding()
            .foregroundColor(.white)
            .background(backgroundColor)
            .cornerRadius(10)
    }
}

struct ContentView: View {
    var body: some View {
        VStack(spacing: 20) {
            CustomButton(title: "Login", backgroundColor: .blue)
            CustomButton(title: "Sign Up", backgroundColor: .green)
        }
        .padding()
    }
}

Customizing Views with Modifiers

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, SwiftUI!")
            .font(.largeTitle) // Increase the font size
            .foregroundColor(.blue) // Change the text color
            .padding() // Give spacing on 4 sides of the text
            .background(Color.yellow) // Background color 
            .cornerRadius(15) // Round the corner
    }
}
struct LabeledContentView: View {
    var body: some View {
        VStack {
            Text("title")
                .font(.largeTitle)
                .padding(.bottom, 5)
            Text("subtitle")
                .font(.subheadline)
                .foregroundColor(.gray)
            Spacer()
        }
        .padding()
    }
}

Practical Examples for UI Components

Example 1: Custom Card View

struct CustomCard: View {
    var title: String
    var subtitle: String
    var icon: String
    
    var body: some View {
        HStack {
            Image(systemName: icon)
                .font(.largeTitle)
                .foregroundColor(.blue)
            
            VStack(alignment: .leading) {
                Text(title)
                    .font(.headline)
                Text(subtitle)
                    .font(.subheadline)
                    .foregroundColor(.gray)
            }
            .padding(.leading)
        }
        .padding()
        .background(Color.white)
        .cornerRadius(10)
        .shadow(radius: 5)
    }
}

struct ContentView: View {
    var body: some View {
        VStack(spacing: 20) {
            CustomCard(title: "SwiftUI", subtitle: "Declarative UI Framework", icon: "swift")
            CustomCard(title: "iOS Development", subtitle: "Build amazing apps", icon: "iphone")
        }
        .padding()
        .background(Color(UIColor.systemGroupedBackground))
    }
}

Button with Multiple Modifiers

struct ContentView: View {
    var body: some View {
        Button(action: {
            print("Button tapped!")
        }) {
            Text("Tap Me")
                .font(.title2)
                .fontWeight(.bold)
                .padding()
                .frame(maxWidth: .infinity)
                .background(Color.purple)
                .foregroundColor(.white)
                .cornerRadius(12)
                .shadow(color: .purple.opacity(0.5), radius: 5, x: 0, y: 5)
        }
        .padding()
    }
}

Working with Stacks

Using VStack

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack(spacing: 20) {
            Text("Welcome to SwiftUI")
                .font(.title)
                .fontWeight(.bold)
                .foregroundColor(.blue)
            
            Text("Create amazing UIs easily!")
                .font(.subheadline)
                .foregroundColor(.gray)
        }
        .padding()
    }
}

Using HStack

struct ContentView: View {
    var body: some View {
        HStack(spacing: 15) {
            Image(systemName: "star.fill")
                .foregroundColor(.yellow)
            
            Text("Favorite")
                .font(.headline)
        }
        .padding()
    }
}

Using ZStack

struct ContentView: View {
    var body: some View {
        ZStack {
            Color.blue // Background color
                .ignoresSafeArea()
            
            Text("SwiftUI")
                .font(.largeTitle)
                .fontWeight(.bold)
                .foregroundColor(.white)
        }
    }
}

Combining Stacks for Complex Layouts

struct ContentView: View {
    var body: some View {
        VStack(spacing: 30) {
            Text("User Profile")
                .font(.largeTitle)
                .fontWeight(.bold)
            
            HStack(spacing: 20) {
                Image(systemName: "person.circle")
                    .font(.system(size: 50))
                    .foregroundColor(.blue)
                
                VStack(alignment: .leading, spacing: 10) {
                    Text("John Doe")
                        .font(.headline)
                    Text("iOS Developer")
                        .font(.subheadline)
                        .foregroundColor(.gray)
                }
            }
            
            ZStack {
                RoundedRectangle(cornerRadius: 10)
                    .fill(Color.green)
                    .frame(height: 50)
                
                Text("Follow")
                    .font(.headline)
                    .foregroundColor(.white)
            }
            .padding(.horizontal)
        }
        .padding()
    }
}

Aligning and Customizing Stacks

struct ContentView: View {
    var body: some View {
        VStack(alignment: .leading, spacing: 10) {
            Text("SwiftUI")
                .font(.headline)
            Text("Declarative UI Framework")
                .font(.subheadline)
                .foregroundColor(.gray)
        }
        .padding()
    }
}

Buttons and Actions in SwiftUI

Designing Buttons

import SwiftUI

struct ContentView: View {
    var body: some View {
        Button("Tap Me") {
            print("Button tapped!")
        }
        .padding()
        .background(Color.blue)
        .foregroundColor(.white)
        .cornerRadius(10)
    }
}

Customizing Buttons with Labels

struct ContentView: View {
    var body: some View {
        Button(action: {
            print("Button with icon tapped!")
        }) {
            HStack {
                Image(systemName: "star.fill")
                    .foregroundColor(.yellow)
                Text("Favorite")
                    .fontWeight(.bold)
            }
        }
        .padding()
        .background(Color.black)
        .foregroundColor(.white)
        .cornerRadius(8)
    }
}

Designing Toggles

struct ContentView: View {
    @State private var isOn: Bool = false
    
    var body: some View {
        Toggle(isOn: $isOn) {
            Text("Enable Notifications")
        }
        .padding()
    }
}

Customizing Toggle Styles

struct ContentView: View {
    @State private var isOn: Bool = true
    
    var body: some View {
        Toggle(isOn: $isOn) {
            Text(isOn ? "Notifications Enabled" : "Notifications Disabled")
                .fontWeight(.bold)
                .foregroundColor(isOn ? .green : .red)
        }
        .toggleStyle(SwitchToggleStyle(tint: .blue))
        .padding()
    }
}

Combining Buttons and Toggles

struct ContentView: View {
    @State private var isDarkMode: Bool = false
    
    var body: some View {
        VStack(spacing: 20) {
            Toggle(isOn: $isDarkMode) {
                Text("Dark Mode")
                    .fontWeight(.bold)
            }
            .toggleStyle(SwitchToggleStyle(tint: .purple))
            
            Button(action: {
                print("Dark Mode is \(isDarkMode ? "Enabled" : "Disabled")")
            }) {
                Text("Confirm")
                    .padding()
                    .frame(maxWidth: .infinity)
                    .background(isDarkMode ? Color.black : Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(8)
            }
        }
        .padding()
    }
}

Handling Text and Images

Styling Text for Different Use Cases

Basic Text Styling

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Welcome to SwiftUI")
            .font(.largeTitle) // Set the font size
            .fontWeight(.bold) // Bold text weight
            .foregroundColor(.blue) // Set the text colour
            .padding() // Add space on 4 sides of text
    }
}

Custom Font and Alignment

struct ContentView: View {
    var body: some View {
        Text("SwiftUI makes UI development fun!")
            .font(.custom("Courier", size: 20)) // Use Custom Font
            .multilineTextAlignment(.center) // Align Text in Center
            .lineSpacing(10) // Increase the line spacing
            .padding()
    }
}

Displaying and Scaling Images with Image

struct ContentView: View {
    var body: some View {
        Image(systemName: "star.fill")
            .font(.largeTitle) // Icon size set 
            .foregroundColor(.yellow) // Set Icon color
    }
}

Combining Text and Images

struct ContentView: View {
    var body: some View {
        VStack(spacing: 20) {
            Image("profile")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 100, height: 100)
                .clipShape(Circle()) // Image ko circle shape mein dikhayein
                .overlay(
                    Circle().stroke(Color.blue, lineWidth: 4) // Circle border add karein
                )
            
            Text("John Doe")
                .font(.headline)
                .fontWeight(.bold)
            
            Text("iOS Developer")
                .font(.subheadline)
                .foregroundColor(.gray)
        }
        .padding()
    }
}

Advanced Image Scaling Techniques

struct ContentView: View {
    var body: some View {
        ZStack {
            Image("landscape")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(height: 300)
                .clipped()
            
            LinearGradient(
                gradient: Gradient(colors: [.black.opacity(0.6), .clear]),
                startPoint: .bottom,
                endPoint: .top
            )
            
            Text("Explore the World")
                .font(.largeTitle)
                .fontWeight(.bold)
                .foregroundColor(.white)
                .padding()
        }
    }
}

Building Lists and Grids

Using List to Display Structured Data

import SwiftUI

struct ContentView: View {
    var body: some View {
        List {
            Text("Swift")
            Text("Kotlin")
            Text("Python")
            Text("Java")
        }
    }
}

Dynamic List with Data

struct ContentView: View {
    let programmingLanguages = ["Swift", "Kotlin", "Python", "Java"]
    
    var body: some View {
        List(programmingLanguages, id: \.self) { language in
            Text(language)
        }
    }
}

List with Custom Rows

struct ContentView: View {
    struct Language: Identifiable {
        let id = UUID()
        let name: String
        let proficiency: String
    }
    
    let languages = [
        Language(name: "Swift", proficiency: "Advanced"),
        Language(name: "Python", proficiency: "Intermediate"),
        Language(name: "Java", proficiency: "Beginner")
    ]
    
    var body: some View {
        List(languages) { language in
            HStack {
                Text(language.name)
                    .font(.headline)
                Spacer()
                Text(language.proficiency)
                    .foregroundColor(.gray)
            }
        }
    }
}

Working with LazyVGrid and LazyHGrid for Grid Layouts

struct ContentView: View {
    let items = Array(1...20)
    let columns = [
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible())
    ]
    
    var body: some View {
        ScrollView {
            LazyVGrid(columns: columns, spacing: 20) {
                ForEach(items, id: \.self) { item in
                    Text("\(item)")
                        .frame(width: 50, height: 50)
                        .background(Color.blue)
                        .foregroundColor(.white)
                        .cornerRadius(8)
                }
            }
            .padding()
        }
    }
}

Combining Lists and Grids

struct ContentView: View {
    let gridItems = [
        GridItem(.adaptive(minimum: 50))
    ]
    
    var body: some View {
        List {
            ForEach(1...5, id: \.self) { section in
                Section(header: Text("Section \(section)").font(.headline)) {
                    LazyVGrid(columns: gridItems, spacing: 10) {
                        ForEach(1...10, id: \.self) { item in
                            Text("\(item)")
                                .frame(width: 50, height: 50)
                                .background(Color.orange)
                                .cornerRadius(8)
                        }
                    }
                }
            }
        }
    }
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *