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:
Create a new SwiftUI project in Xcode.
Add your image to the Assets.xcassets folder:
- Drag and drop
foo.png
into the Assets.xcassets folder in your Xcode project.
- Drag and drop
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()
}
}
- 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.
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