Let’s break down how to create the first weather app UI image in SwiftUI, incorporating stacks, alignment, and padding for a clean layout:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack(alignment: .center, spacing: 20) {
            // Header
            HStack {
                Image(systemName: "sun.max.fill") // Replace with your app logo
                    .font(.system(size: 30))
                Text("WeatherApp")
                    .font(.title)
                    .fontWeight(.bold)
            }
            .padding(.top, 20)
            
            // Main Content (Weather Information)
            VStack(alignment: .center, spacing: 10) {
                Text("25°C")
                    .font(.system(size: 60, weight: .thin))
                Text("Sunny")
                    .font(.title2)
                HStack(spacing: 20) {
                    WeatherDetail(iconName: "humidity.fill", value: "60%")
                    WeatherDetail(iconName: "wind", value: "10 km/h")
                }
            }

            // Forecast (Next Days)
            HStack(spacing: 20) {
                ForecastDayView(day: "Wed", iconName: "cloud.sun.fill", temperature: "22°")
                ForecastDayView(day: "Thu", iconName: "sun.max.fill", temperature: "26°")
                ForecastDayView(day: "Fri", iconName: "cloud.rain.fill", temperature: "18°")
            }

            // Footer (Location & Time)
            HStack {
                Text("Lahore, Pakistan")
                    .font(.caption)
                Spacer()
                Text("Updated: 10:15 AM")
                    .font(.caption)
            }
            .padding(.bottom, 20)
        }
        .padding(.horizontal, 20) // Overall horizontal padding
    }
}

// Reusable Views
struct WeatherDetail: View {
    let iconName: String
    let value: String

    var body: some View {
        HStack(spacing: 5) {
            Image(systemName: iconName)
            Text(value)
        }
    }
}

struct ForecastDayView: View {
    let day: String
    let iconName: String
    let temperature: String

    var body: some View {
        VStack(spacing: 5) {
            Text(day)
            Image(systemName: iconName)
                .font(.system(size: 20))
            Text(temperature)
        }
    }
}

#Preview {
    ContentView()
}

Key Points:

  • VStack and HStack: Used to create the vertical and horizontal layout structures.
  • alignment: Aligns the content within the stacks (e.g., .center for centering).
  • spacing: Adds space between elements within the stacks.
  • padding: Adds padding around the edges of views.
  • Reusable Views: WeatherDetail and ForecastDayView to keep the code organized and maintainable.

Customization:

  • Replace the placeholder Image(systemName: "sun.max.fill") with your app logo.
  • Update the weather data (25°C, “Sunny”, etc.) with actual values from your API or data source.
  • Customize the colors and fonts to match your app’s design.

Main Structure (ContentView):

  • VStack(alignment: .center, spacing: 20): The entire content is organized within a vertical stack, where elements are centered horizontally and have 20 points of spacing between them.
  • HStack: Used to create horizontal layouts within the main VStack.
  • Text: Displays text content like “WeatherApp,” temperature, conditions, etc.
  • Image(systemName:): Renders system icons for the weather app logo and weather details.

2. Header:

  • HStack: Arranges the app logo and name horizontally.
  • Image(systemName: "sun.max.fill"): Displays the sun icon (replace with your app logo).
  • Text("WeatherApp"): Displays the app name in a larger, bold font.
  • .padding(.top, 20): Adds padding to the top of the header.

3. Main Content (Weather Information):

  • VStack(alignment: .center, spacing: 10): Arranges the temperature, weather condition, humidity, and wind information vertically.
  • Text("25°C"): Displays the temperature in a large font.
  • Text("Sunny"): Displays the current weather condition.
  • HStack(spacing: 20): Arranges humidity and wind details horizontally.
  • WeatherDetail: A custom view for displaying weather details with an icon and value.

4. Forecast (Next Days):

  • HStack(spacing: 20): Arranges the forecast views for the next three days horizontally.
  • ForecastDayView: A custom view for displaying a day’s forecast with the day name, icon, and temperature.

5. Footer (Location & Time):

  • HStack: Arranges the location and time horizontally.
  • Text("Lahore, Pakistan"): Displays the location.
  • Spacer(): Pushes the time to the right edge.
  • Text("Updated: 10:15 AM"): Displays the time of the last update (you’ll need to replace with the actual time when fetching data).

6. Reusable Views:

  • WeatherDetail: Takes an icon name and value as input and displays them together (e.g., humidity icon and “60%”).
  • ForecastDayView: Takes a day name, icon name, and temperature as input and displays them in a vertical layout.

7. Padding and Spacing:

  • .padding(.horizontal, 20): Adds horizontal padding to the entire ContentView.
  • .padding(.top, 20): Adds top padding to the header.
  • .padding(.bottom, 20): Adds bottom padding to the footer.

Additional Notes:

  • The preview uses #Preview { ContentView() } to show how the UI will look in Xcode.
  • The temperature, weather condition, location, and time are placeholders. You’ll need to replace these with actual data fetched from a weather API.
  • Remember to customize the icons, fonts, and colors to match your design preferences.