When rendering multiple SwiftUI previous, they always render in a vertical stack. I would like to lay them out horizontally or ideally on a grid., because I have more horizontal space on my screen.
It's unproductive to have to keep scrolling up-and-down between multiple the previews, especially when you have more than two.
Anyone know a work around, or if it's possible?
Update: E.g. As you see in the screen shot, I want to those two previews side by side horizontally.
Xcode Version 12.0 beta (12A6159)
A grid layout can be seen in almost all Apple applications. You may have noticed it in the Photos app or the Calendar app. It helps to accommodate more vital data into the same space by splitting the views into rows and columns. To demonstrate how the SwiftUI grid layout works, we’ll build a reminder app.
SwiftUI’s core layout engine works by asking each child view to determine its own size based on the bounds of its parent, and then asks each parent to position its children within its own bounds.
LazyVGrid contains the following parameters for customization: the column to position each item, alignment in the view, the spacing between grid and next item in the view, and pinned views to bound to the scroll view. We’ll start by adding a vertical scroll view that will house the grid.
Next, let’s take a look at how padding works in SwiftUI. Just like in other layout systems, like CSS, padding enables us to offset the contents of a view within its own frame. However, depending on where in our chain of view modifiers we apply our padding, we can get quite different results.
You can achieve it like this. This way it doesn't show iPhone borders but views render it as how it will look on iPhone 11 Pro screen.
struct SidebarView_Previews: PreviewProvider {
static var previews: some View {
HStack {
//... All your views ...
}.previewLayout(.fixed(width: 375 * 2, height: 812))
}
}
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