11

I want to have a full screen image in the background. I have implemented this:

struct LoginView: View {
    var body: some View {
        VStack {
            Spacer();
            Text("Hallo");
            Text("Hallo2");
            Text("Hallo2");
            Text("Hallo2");
            Text("Hallo2");
            Text("Hallo2");
            Text("Hallo2");
            Spacer();
            }.background(Image("Background LaunchScreen")
                .resizable()
                .aspectRatio(UIImage(named: "Background LaunchScreen")!.size, contentMode: .fill)
                .clipped())
            .edgesIgnoringSafeArea(.all)
    }
}

When I remove the spacers, the image is no longer displayed in full screen mode.
Surely that can be solved differently?

And if I turn the iPhone in the simulator to the side, I have left and right white stripes.
How can I change this?

3 Answers 3

29

Here's a possible solution using GeometryReader and ZStack:

import SwiftUI

struct LoginView: View {
    var body: some View {
        GeometryReader { geometry in
            ZStack {
                Image("LaunchImage")
                    .resizable()
                    .aspectRatio(geometry.size, contentMode: .fill)
                    .edgesIgnoringSafeArea(.all)
                VStack {
                    ForEach (1...10, id: \.self) { _ in
                        Text("Hallo")
                            .foregroundColor(Color.white)
                    }
                }
            }
        }
    }
}

#if DEBUG
struct LoginView_Previews: PreviewProvider {
    static var previews: some View {
        LoginView()
    }
}
#endif

Results

portrait landscape

Sign up to request clarification or add additional context in comments.

4 Comments

above solution worked for me could you pls tell me why you are using GeometryReader and then ZStack because i also tried below code ZStack { Image("bgImg") .resizable() .scaledToFill() .edgesIgnoringSafeArea(.all) } which worked for me so what differences in that
@Dilip At that time (Aug 25 '19), that was the only way I could get it to work. If it now works without GeometryReader, that's even better then. :)
thanks a lot i am using your answer only, thanks for reply
This makes the image to increase the ZStack and if I align something to the left it starts from out of the screen
2

Using only ZStack:

import SwiftUI

struct ContentView: View {
    var body: some View {
        ZStack{
            Image("bg")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(minWidth: 0, maxWidth: .infinity)
                .edgesIgnoringSafeArea(.all)
            VStack {
                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundStyle(.tint)
                Text("Hello, world!")
            }
            .padding()
        }
    }
}

#Preview {
    ContentView()
}

enter image description here

Comments

0

To get my VStack correct I had to do this: ZStack(alignment: .leading)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.