SwiftUI Form style changed when embedded in UIHostingViewController

947 Views Asked by At

I have A SwiftUI view which is a standard form and in canvas it has the built in IOS form style which is the look I desire.

However this form is being used a SideBar embedded in a UIHostingViewController which is a child of a UIViewController. Everything works as expected however the styling of the form is replaced by that of a normal UITableViewController.

I have not added any code because my struct is a simple form with multiple sections.

Please refer to the images below

Expected Look as it shows in canvas enter image description here

Actual look after being embedded in UIHostingViewController SideBar

2

There are 2 best solutions below

0
OverD On BEST ANSWER

I was able to fix this issue by simply changing the widthAnchor of the form and allowing it more size.

I noticed that when I rotated my device and the UIHostingViewControllers frame changed its widthAnchor and the SwiftUI view rendered correctly on screen.

1
ScottM On

The Form element in SwiftUI is effectively a wrapper around List. The default list style is context-dependent, but if you add a .listStyle modifier that will generally be observed.

You don't have any example code in your question, or indeed a visual of what you expect the form to look like. But I'd hazard a guess that you'd like the form to look like it does in a standard iPhone (not SE) width? In which case you should try

Form {
  // your form elements
}
.listStyle(.insetGrouped)

It's possible that the .sidebar list style might also be more appropriate.