I am trying to center an image within the screen. It goes should fit the height but stretch the width outside the screen to keep it proportional. The image properly sizes and centers itself normally, but as soon as I add .edgesIgnoringSafeArea(.all)
the image gets off-centered. I don't know if this is a bug, but I am just starting up with it SwiftUI so there may be a simple answer.
.edgesIgnoringSafeArea(.all)
struct ContentView: View {
var body: some View {
Image("background_rain")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
}
}
Output:
.edgesIgnoringSafeArea(.all)
struct ContentView: View {
var body: some View {
Image("background_rain")
.resizable()
.aspectRatio(contentMode: .fill)
.edgesIgnoringSafeArea(.all)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
}
}
Output:
Thank you in advance
How To Center Images Step 1) Add HTML: Example <img src="paris.jpg" alt="Paris" class="center"> Step 2) Add CSS: To center an image, set left and right margin to auto and make it into a block element: Example .center... W3.CSS Tutorial
To center an image with CSS Grid, wrap the image in a container div element and give it a display of grid. Then set the place-items property to center. P.S.: place-items with a value of center centers anything horizontally and vertically. You can also center an image by setting a left and right margin of auto for it.
The introduction of CSS Flexbox made it easier to center anything. Flexbox works by putting what you want to center in a container and giving the container a display of flex. Then it sets justify-content to center as shown in the code snippet below: P.S.: A justify-content property set to center centers an image horizontally.
Centering with the text-align property works for block-level elements only. So how do you center an image with the text-align property? You wrap the image in a block-level element like a div and give the div a text-align of center.
Here is a solution
Color.clear.overlay(
Image("background_rain")
.resizable()
.aspectRatio(contentMode: .fill)
)
.edgesIgnoringSafeArea(.all)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With