|
Hi, happy Monday! I have a scrollview with a few subviews, and I wanted each of these subviews to have a certain frame height relative to UI screen height. I know you can do this with GeometryReader, but the moment I put GeometryReader inside ScrollView, it fails to read screen height. Here's what I want to achieve Without scrollview, this works nicely.
But the view breaks when I use ScrollView. I tried putting ScrollView inside Geometry Reader, and the other way around, but with the same results (geo.size.height becomes 0 it seems).
So is there a way to set frame height relative to screen height for frames inside a scrollview? Or will I have to just resort to |
|
Just guessing here. The ScrollView adapts automatically to the content it has and has no fixed height you can measure before there is content. Basically, it could scroll to infinity. The whole point of a ScrollView is that you can scroll down when you have more content than it can fit on the screen. And as your rectangles don't have content it shrinks them. What happens if you remove the height from your rectangles completely? |
|
You can also specify a minimum, as well. For example
As you are only scrolling in the vertical direction, the width parameters are not needed. So it can be simplified to.
Adding a couple more rectangles, with different colours to show the difference, to the scroll view.
|
|
This taken from Paul's Pro SwiftUI book (Which is on offer until 4 Dec)
So to try and explain a little about what happening in your code.
That will fill the screen with three But when you add a
So it the PS |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.