Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove SwiftUI Form padding

When you use a SwiftUI Form it creates padding on the leading edge, which is fine for Form input but I would like to add additional content to the Form e.g. an image that takes the entire width of the screen, but because the image is in the Form, padding gets applied and the image gets pushed off screen slightly. How can I remove all Form padding?

struct MyForm: View {
  var body: some View {
    Form {
      Image(uiImage: someImage)
        .resizable()
        .aspectRatio(contentMode: .fill)
        .frame(width: UIScreen.main.bounds.width)

      TextField("Name", text: $name)

      // Other fields
    }
  }
}

I know that the underlying view for Form is a UITableView where I can do things like UITableView.appearance().backgroundColor = .clear to change the Form appearance, but I can't figure out how to remove the leading padding.

I also know I can move the Image view outside the Form and put everything in a stack, but that creates other issues with scrolling that I'd like to avoid.

like image 959
Darkisa Avatar asked Mar 03 '23 08:03

Darkisa


1 Answers

Here is a solution. Tested with Xcode 11.4 / iOS 13.4

  Image(uiImage: someImage)
    .resizable()
    .aspectRatio(contentMode: .fill)
    .listRowInsets(EdgeInsets())        // << this one !!

Note: hardcode to UIScreen.main.bounds.width is not needed

like image 126
Asperi Avatar answered Mar 11 '23 17:03

Asperi