I want to make my code inside a scrollview based on a boolean
Something like this :
ScrollView{
Vstack {
}
Hstack {
}
}.isWrapped(true)
So if the bool is true, the content is wrapped inside a ScrollView, if not, I just get the content as is. (without the scrollView parent)
I tried with multiples way, the only thing I see as possible is 2 blocks of code, not good practice thought.
3
Answers
Because of the nature of SwiftUI, you can’t remove the parent without removing children. As you mentioned in your edit, this will require two blocks of code. But, you can make it much more friendly in two different ways.
The first way is a reusable component. It takes a Binding
isVisible
variable and the content.To use this new component is to simply replace the use of
ScrollView
in the area that you want to manually adjust. I.E:But this is not your only option.
If you want to keep things as simple as possible, you can achieve this using a simple
ViewBuilder
. Create the view you don’t want to change inside theViewBuilder
, and then create a condition around theScrollView
, all the while placing the variable as the content. It would look as follows:As you’ve probably come to realize, this is one of the limitations of SwiftUI. But it can be considered a strength because you will always know if a view’s parent is there or not.
I hope this little tidbit helped, happy coding!
You can also make it even easier:
One way is to always use a
ScrollView
, but tell it not to scroll. If you pass an empty set as the first argument toScrollView
, it won’t scroll.Another way is to extract the content back out of the
ScrollView
in your proposedisWrapped
modifier, like this: