Skip to content

Instantly share code, notes, and snippets.

@sowenjub
Created February 28, 2024 08:34
Show Gist options
  • Save sowenjub/8adec1bc883cfbbb6a9b84f60fc089c5 to your computer and use it in GitHub Desktop.
Save sowenjub/8adec1bc883cfbbb6a9b84f60fc089c5 to your computer and use it in GitHub Desktop.
ScrollViewThatFits
import SwiftUI
struct ScrollViewThatFits<Content: View>: View {
var showsIndicators: Bool
@ViewBuilder
var content: Content
init(showsIndicators: Bool = false, @ViewBuilder content: () -> Content) {
self.showsIndicators = showsIndicators
self.content = content()
}
var body: some View {
ViewThatFits(in: .vertical) {
content
ScrollView(showsIndicators: showsIndicators) {
content
}
}
}
}
#Preview("Plain Content") {
ScrollViewThatFits {
Text("Hello, world!")
}
}
#Preview("ScrollView") {
ScrollViewThatFits {
Text(
"""
In the enchanted forest of SwiftUI, where code snippets flutter like leaves in the wind, there lived two magical elements: ScrollView and ViewThatFits. These were not your ordinary spells cast from the Book of UI Elements; they were special, each with their own unique charm and purpose.
ScrollView was a whimsical pathway, stretching far and wide across the vast landscapes of Contentland. It invited adventurers to stroll at their leisure, unveiling endless treasures of text, images, and views that lay hidden beyond the horizon of the visible frame. "Scroll with me," it whispered, "and discover the infinite stories that await in the kingdom of Overflow."
Meanwhile, ViewThatFits, the shape-shifting wizard of the realm, was a master of adaptation. It had the extraordinary ability to morph itself into the most fitting form, ensuring that its content was always showcased in the best light. "Let me adjust," it proclaimed, "for I shall find the perfect fit between the constraints of the Outer Frame and the desires of the Inner Content."
One day, a curious developer embarked on a quest to build a mystical interface that could showcase an array of elements in a land of limited space. "How do I choose between these two powerful enchantments?" the developer pondered. It was then they had a brilliant idea: why not combine the magic of ScrollView with the adaptability of ViewThatFits?
And so, they cast a spell, intertwining the essence of ScrollView's endless path with ViewThatFits' shape-shifting prowess. The result was a marvel to behold: a layout that could extend infinitely, yet always present its content in the most fitting manner. The developer exclaimed, "Eureka! I have created the ultimate scrollable realm where content adapts seamlessly, ensuring no story is ever cut short, and every tale finds its place."
And thus, the legend of ScrollView and ViewThatFits spread across the land, a testament to the developer's ingenuity and the magic of SwiftUI. They lived happily ever after in the codebases of many, bringing joy and utility to all who summoned them.
So, dear reader, next time you embark on a journey through the forests of SwiftUI, remember the tale of ScrollView and ViewThatFits. With these enchantments by your side, your interfaces will surely live ever after in the hearts of users far and wide.
"""
).padding()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment