Search code examples
swiftimageswiftuiscrollviewpadding

SwiftUI Image AspectFit ratio not working properly with screen width


I am trying to keep image width equal to screen with aspectfit ratio.I am adding Text on remaining screen height.It is working well untill text height touch the bottom view line.There is unexpected auto left and right space is added around image.It is more visible if i use the same view in tab bar.

I also use GeometryReader and define some area to image and text but unfortunetely it's also not working.

I try it with other combination like Image and ScrollView , Image and List but still no luck.

struct ContentView: View {
        var body: some View {
            VStack(spacing: 0) {
                        Image("tickimg")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(minWidth: UIScreen.main.bounds.size.width)
                            .background(Color.blue)
                            .border(Color.yellow)
                        Text("HelloWorld\n\n\n\n\n\n\n\n\n\n\n\n\\n\n\n\n\n\n\n\n\nn\n\n\n").background(Color.red)
        }
    }
}

Extra space on left and right side

Here is complete project link https://github.com/umair-Ahm/ImagePadding

Is it possible to acheive it without spacing


Solution

  • struct ContentView: View {
            var body: some View {
                VStack(spacing: 0) {
                    ZStack {
                        Image(systemName: "checkmark.circle.fill")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                        
                    }
                    .frame(height: UIScreen.main.bounds.width)
                    .background(Color.blue)
                    .border(.yellow)
    
                    Text("HelloWorld\n\n\n\n\n\n\n\n\n\n\n\n\\n\n\n\n\n\n\n\n\nn\n\n\n").background(Color.red)
                }
            }
    }
    

    enter image description here