Creating an Interactive Plant Gallery with SwiftUI’s Advanced Grid Showcase View

Abdullah Bilgin
Swift Insights
Published in
4 min readAug 19, 2023

Introduction:
In app development, bringing visual content to life is a fascinating challenge. Imagine showcasing a collection of captivating plant images in a gallery that dynamically switches between grid and list views with a touch of a button. With SwiftUI’s powerful tools, this dynamic experience can be achieved effortlessly. In this story, I’ll explore the mesmerizing “AdvancedGridShowcaseView” component, uncovering how it seamlessly toggles between grid and list displays while providing an immersive and interactive user experience.

Companent Preview Gif

The Power of Visuals:
Visual content speaks volumes, and presenting it in a user-friendly way is key. Our journey begins with the "AdvancedGridShowcaseView" component, designed to display various plant images beautifully. Thanks to the decoding magic of SwiftUI, the component effortlessly loads plant data from a JSON file named "plants.json". This data, containing plant names and corresponding images, becomes the foundation for our visual feast.

{
"id": "cactus",
"name": "Cactus",
"image": "cactus",
"description": "Cacti are succulent plants adapted to arid climates."
}
var plants: [Plant] = Bundle.main.decode("plants.json")

Switching Views with Ease:
Imagine having the ability to switch between grid and list views effortlessly. SwiftUI’s state management makes this a breeze. Using the isGridViewActive state property, the component effortlessly toggles between two different modes. When isGridViewActive is set to true, a dynamic grid layout emerges. Conversely, setting it to false transforms the view into a neat and organized list. This seamless transition showcases the power of SwiftUI in creating diverse user experiences.

@State private var isGridViewActive: Bool = true

Dynamic Grid Layout:
Flexibility is essential in creating engaging user interfaces. The “AdvancedGridShowcaseView” component embraces this by allowing users to customize the number of columns in the grid view. This feature demonstrates the incredible versatility of SwiftUI in catering to user preferences.

  func gridSwitch() {
// Switch the grid layout and update grid column count
gridLayout = Array(repeating: .init(.flexible()), count: gridLayout.count % 3 + 1)
gridColumn = gridLayout.count

// Update the toolbar icon based on the current grid column count
switch gridColumn {
case 1:
toolbarIcon = "square.grid.2x2"
case 2:
toolbarIcon = "square.grid.3x2"
case 3:
toolbarIcon = "rectangle.grid.1x2"
default:
toolbarIcon = "square.grid.2x2"
}
}
      .toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
HStack {
// Button to switch to List view
Button(action: {
isGridViewActive = false
haptic.impactOccurred()
}) {
Image(systemName: "square.fill.text.grid.1x2")
.font(.title2)
.foregroundColor(isGridViewActive ? .PrimaryColor : .SecondaryColor)
}

// Button to switch to Grid view
Button(action: {
isGridViewActive = true
haptic.impactOccurred()
gridSwitch()
}) {
Image(systemName: toolbarIcon)
.font(.title2)
.foregroundColor(isGridViewActive ? .SecondaryColor : .PrimaryColor)
}
} //: HStack
}
}

Engaging Interactions with Haptic Feedback:

User interaction is at the heart of any engaging app experience. SwiftUI's integration of the UIImpactFeedbackGenerator class takes user interaction to the next level. Each tap on a plant image triggers subtle haptic feedback, creating a tactile connection between the user and the interface. This small detail adds a layer of immersion that enhances the overall experience.

    var haptic = UIImpactFeedbackGenerator(style: .medium)

Display Logic and Navigation:
The “AdvancedGridShowcaseView” component dynamically updates its display as users explore the plant gallery. A scrollable view unfolds in grid mode, with each plant image elegantly positioned within the grid layout. Alternatively, switching to list view presents a sleek list of plant items. Navigation is seamless with SwiftUI’s NavigationView, providing a polished touch to the user journey.

Personalizing the Experience:
No dynamic component is complete without a touch of personalization. The “AdvancedGridShowcaseView” component allows users to initiate their journey with a randomly selected plant image. This small yet delightful touch ensures that every encounter with the gallery is unique.

Conclusion:

The “AdvancedGridShowcaseView” component epitomizes the synergy between visuals, user interactions, and code logic in SwiftUI. Through loading, switching views, customizing layouts, and engaging interactions, we’ve explored the power of SwiftUI’s features in creating an immersive plant gallery experience. As you embark on your SwiftUI adventures, remember that each line of code is a brushstroke contributing to a canvas of captivating user experiences.

GitHub Repository :

Project Board:

Thank you for joining me in exploring the ‘AdvancedGridShowcaseView’ component. As we’ve delved into the world of interactive plant galleries, I hope you’ve gained insights into how SwiftUI empowers developers to create dynamic and engaging user experiences. Whether you’re a seasoned developer or just starting, remember that every line of code is a step toward crafting captivating interfaces. If you have any questions or thoughts, I’d love to hear them in the comments below. Happy coding!

--

--

Abdullah Bilgin
Swift Insights

"iOS engineer & IT head, crafting code & innovation. Leading with tech prowess & strategic vision. Bridging iOS dev & IT realms. Lifelong learner. 📱💡"