Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout multiple SwiftUI previews horizontally (or in grid) instead of vertically?

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)

enter image description here

like image 330
Antony Stubbs Avatar asked Jun 30 '20 16:06

Antony Stubbs


People also ask

What is the SwiftUI grid layout?

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.

How does SwiftUI work?

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.

How do I customize the lazyvgrid view?

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.

What is padding in SwiftUI and how does it work?

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.


1 Answers

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))
    }
}
like image 135
nickinade Avatar answered Oct 20 '22 17:10

nickinade