Code Monkey home page Code Monkey logo

gridstack's Introduction

📱GridStack

A flexible grid layout view for SwiftUI.

WWDC20 Update

Apple  released LazyVGrid and LazyHGrid at WWDC20.

If you are fine to only support i(Pad)OS 14, macOS 11, tvOS 14, watchOS 7 respectively those ^-- are definitely the way to go.

If you want to support i(Pad)OS 13, macOS 10.15, tvOS 13, watchOS 6 keep on reading.


Release Build & Test

📱 iOS 13+, 💻 macOS 10.15+, 📺 tvOS 13+, ⌚ watchOS 6+

Simply pass the minimum width the grid cells should have and the spacing between them and it will adjust depending on the available width.

So writing this:

Code

will give you you this:

Screenshot 2019-07-14 at 14 07 02

It also adjusts correctly when the device is rotated:

rotation

🗺 Usage Overview

Think of the grid in the way of what is the minimum width you want your cells to be. That way it is easy to adjust to any available space. The only other size you need to provide is the spacing between the cells.

To actually create the grid we need to know the numbers of items. Then the content view builder will be called with each index and the cellWidth that you can then pass to the frame of whatever you want to display inside.

👕 Sizing your views inside the cells

The grid will wrap each item you provide with in a view that gets the cellWidth set as width. No height constraint is put on the cell. That is so that you can size your content as flexible as possible. Here are just a couple of examples what you can do.

Height defined by content

GridStack(...) { index, cellWidth in
    Text("\(index)")
        // Don't pass any height to the frame to let it be defined by it's content
        .frame(width: cellWidth)
}

Square items

GridStack(...) { index, cellWidth in
    Text("\(index)")
        // Pass the cellWidth as width and height to the frame to make a square
        .frame(width: cellWidth, height: cellWidth)
}

Aspect Ratio items

GridStack(...) { index, cellWidth in
    Text("\(index)")
        // Pass the cellWidth as width and a portion of it as height to get a certain aspect ratio
        .frame(width: cellWidth, height: cellWidth * 0.75)
}

✍️ Signature

GridStack(
    minCellWidth: Length,
    spacing: Length,
    numItems: Int,
    alignment: HorizontalAlignment = .leading,
    content: (index: Int, cellWidth: CGFloat) -> Void
)

📝 Mentions

I created GridStack by taking ideas from FlowStack by John Susek.

gridstack's People

Contributors

barrault01 avatar danurna avatar pietropizzi avatar stevestreza avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

gridstack's Issues

Card Design

Great Framework,
For my App, I'm trying to recreate a similar Card design you used in the Preview Pictures for this library. Could you share the code you used to create the Card Views? Would be really Awesome

Thanks Alot!

HELP WANTED. How to use your GridStack with Dictionary

I have this simple code to test your GridStack, but it doesn't work as I wish.

import GridStack
import SwiftUI

struct ContentView: View {
    
    let singers = ["Andrea","Alessia","Kofi", "Abena", "Efua", "Kweku", "Akosua", "Alberto", "Erika", "Antonio", "Anastasia","Annarita","Anna"]
    
    @State var groupedByFirst: [Character : [String]] = [" " : [""]]
    
    var body: some View {
        ForEach(self.groupedByFirst.keys.sorted{$0<=$1}, id: \.self) { char in
            Section(header: Text("Section: \(String(char))")) {
                NavigationView{
                    GridStack(minCellWidth: 100, spacing: 5, numItems: self.groupedByFirst[char]!.count, alignment: .leading) { index, width in
                        NavigationLink(destination: Text(self.returnArrayOfString(char: char)[index])) {
                            CellView(string: self.returnArrayOfString(char: char)[index])
                        }
                    }
            }
            .navigationViewStyle(StackNavigationViewStyle())
        }
        .onAppear {
            self.groupedByFirst = Dictionary(grouping: self.singers, by: { $0.first! })
        }
    }
   
    func returnArrayOfString(char:Character) -> [String] {
        return self.groupedByFirst[char]!
    }
}

I wish the items (for example the images) were presented as in Photos (for example for the dates) and by clicking on it you can switch to another view, but I cannot make it work.
Can You help me?

Height is not compact but full

The library is based on ScrollView, which by default is full-screen height. Is there a better way to make its height as compact as possible without manually assigning frame height?

image

Invalid Frame on iOS 14

I'm getting a "[SwiftUI] Invalid frame dimension (negative or non-finite)." error for each cell in iOS 14.

GridStack used inside a VStack

Hi!

So I'm in a situation where I want a horizontal list of items that also stack verticaly when too much items are sent to it, and your tool is perfect for that job <3

...but! I also want it to be on top of a multiline text view, so I put both in a VStack.

The thing is that by doing that, the GridStack and the text view take each half the VStack's height. I looked into the GridStack's code because I expected it to take as small space as possible but I came accros a GeometryReader surrounding the elements, and this is understandable because you need to know the available width to work.
But I know the GeometryReader doesn't shrink to it compact size but rather always take all the space it can, wether its content takes less or not. And it all made sense then!

I've copied your GridStack locally in my project to work on it, and by removing the GeometryReader and sending the width to the GridStack (and removing the ScrollView too because of the same behavior), it all worked perfectly for my intention!

So here is (finaly) my proposal:

  • making the scroll feature optional
  • removing the GeometryReader and let the "upper" view sending the desired width to the GridStack
  • (optionnaly, for the sake of easing the use of most cases) using an enum as the width with a case like "auto" that will add your current GeometryReader so that the "upper" view won't have to use a GeometryReady if it deosn't need to

Here is the result I targeted, the GridStack is (obviously) the row of circles
Capture d’écran 2021-02-19 à 18 25 01

How to expand GridStack to its full intrinsic height?

My cell looks like this:

struct CollectionCell: View {
    var type: PokemonTypes
    
    var body: some View {

        HStack {
            Image(type.imageName)
            
            Divider()

            GeometryReader { geometry in

                GridStack(minCellWidth: geometry.size.width / 2,
                          spacing: 0,
                          numItems: self.type.superEffectiveAgainst.count) { index, cellWidth in
                    
                            Image(self.type.superEffectiveAgainst[index].imageName)
                            .frame(width: cellWidth)
                }
            }
        }
    }
}

And it looks like that in the preview:
Bildschirmfoto 2020-07-24 um 12 50 15

The problem is that it shows only one row and I can scroll inside the table row when there are more than 2 items.
How can I expand the GridStack to its full height?

Thanks!

The Animation when the device rotated

Why not create a smooth animation for this framework.
My Requirement: When the device rotated, all of the grid will move smoothly to its new position.

Thanks.

Not all shapes are showing

When i try to put a Circle() or Rectangle() inside the gridview these items do not show. When I add a button or text, they do show. Any idea what can be wrong?

Btw .. When pairing the circle with text or button in a stack, both items show.

cellWidth is negative

Screen Shot 2564-03-12 at 17 58 13

struct ContentView: View {
    
    // Setup
    init() {
        UITableView.appearance().backgroundColor = .clear
        // UITableViewCell.appearance().backgroundColor = .clear
    }
    
    var body: some View {
        NavigationView{
            Sidebar()
                .navigationTitle("Recon")
            GridStack(minCellWidth: 300, spacing: 2, numItems: 15) { (index, cellWidth) in
                Text("\(index)")
                    .frame(width: cellWidth, height: 100)
                    .background(Color.blue)
            } 
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.