SwiftUI App, Centered Image

TODO: collapasable, copyable, download, etc

Can you generate me a simple iPhone swiftui app that launches and it fills the screen with a photo from Assets 'foo.png'. It should be centered and fill the entire screen by matching the width of the photo to the width of the phone screen (viewport?).Here's a simple SwiftUI app that displays an image from the Assets folder named "foo.png," centered and filling the entire screen by matching the width of the image to the width of the phone screen:

  1. Create a new SwiftUI project in Xcode.

  2. Add your image to the Assets.xcassets folder:

    • Drag and drop foo.png into the Assets.xcassets folder in your Xcode project.
  3. Modify the ContentView.swift file:

import SwiftUI

struct ContentView: View {
    var body: some View {
        GeometryReader { geometry in
            Image("foo")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: geometry.size.width, height: geometry.size.height)
                .clipped()
        }
        .edgesIgnoringSafeArea(.all)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
  1. Run the app:
    • Make sure the target device is selected (e.g., iPhone 14), then click the "Run" button (or press Cmd+R) to build and run the app.

This code uses GeometryReader to get the size of the screen and adjusts the image to fill the entire screen while maintaining its aspect ratio. The edgesIgnoringSafeArea(.all) ensures that the image fills the entire screen, including the area behind the status bar and the home indicator.

SwiftUI App, Centered Image created at Tue Jan 20 1970 20:48:25 GMT+0000 (Coordinated Universal Time)

Related Chats

Frame Photos with SwiftUI 0.559

Draw Rects & Ovals 0.554

Lightsaber Trainer App 0.548

New chat 0.529

Reusable SwiftUI custom view. 0.524

ToggleButton con immagini SwiftUI 0.519

Pass Data to SwiftUI 0.517

Control Window Size: VisionOS 0.504

SwiftUI iOS App: Features 0.497