|
i was watching project 18 about GeometryReader and noticed the height of the Text looked really off basically if I have this
it renders likes this my question is why is the height of the Text view so tall? Its also bottom aligning itself? If I remove the Geometry reader the height of the Text looks like what it should be |
|
|
|
hmm I get that that the the geomertyReader takes all the space its given. but this looks like a bug in swiftUI. maybe drawing the geometryReader bounds will make it more clear
In the follwing image can you explain what the red backround represents vs the green Border? my understanding is that green border is the space that geometryReader was given, and is also the same space that is offered to the textView. the textView then picks a bounds which should just be enough to cover the text. Then the geometry reader should position the textView in the topleft. But swiftUI just decides to render some extra red for no reason at the topleft. The red also goes outside the safeArea too which is wierd |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
This is the way it currently works 😂😂😂 sounds strange but this is the reality we all living in. Sometimes this is weird but what to do. So let me try to explain what is happining here. Backgrounds (in your case red color) will ignore all safe area edges by default. Simply putting your backgound color ignores the safe area and it expands itself up. To prevent this, explicitly specify a different safe area edge to ignore - >
|
|
As for the green vs red -> green this is so called safe area where you can draw your UI. Beyond green is area needed by the system for different parts of iOS interface tabs, indicators you name it, and it is so to say unsafe. So by default all views that take up all available space do not go beyond that area unless you explicitly make it go further, using .ignoresSafeArea() modifier. As for your example you use geometry reader. When you use it, all items inside it will be placed at x: 0 y: 0 coordinates of geometry reader, not in the center as many views do. So here you created geometry reader which placed your text in the upper left corner. You placed red backround attached to text. That background ignores safe area by default and expands that color up. |
|
@ygeras even if you do Also In apple wwdc video they specially say background is layout neutral? Was it an oversight? |
|
@Ahbee I am trying to explain you what it does and not why it does this particluar way. With the post above I explained you that by default backgrounds ignore safe area and again you are making the same with Put it simply if you write:
Let's assume it that string takes up 100 by 50 frame - that's it it is the size of the view that will be placed in UI. You attach later background to that text view, it also takes up the same size. How do you expect to fill out all the entire area of the screen, when you said to take only 100 by 50? Just for the sake of the experiment remove geometry reader from your code and you will see text view with red background in the middle of the screen. Right? When you used geometry reader - it moved your text to left top. In that position, background touches the line where safe area starts, unless you say NOT to ignore it, it will cross that line and goes up. Hope it makes some sense now :) |
|
To make it even more clearer, let me show you some more examples ->
If you attach background to geometry reader NOT to text view, it will fill up all available space on the screen. As background ignores safe area edges by default. BUT if you use this ->
you will see that color fills up all the area that geometry reader takes, BUT not safe area! |
|
@ygress thanks I think Im just confused on how safeArea restictions fit in to the 3 layout rules of SwiftUI
if someone could explain what is going on in those 3 rules it would clear my understanding take this code
the heiercahcy looks something like this GeometryReader has 1 child Background. Background then has 2 children Text and Color.Red. Text and Color.red are leaf nodes here is my understanding
|
|
@Ahbee let me use Paul's explanation and adjust it to your needs If we put this into the three-step layout system, we end up with a conversation a bit like this:
AND REMEMBER that when you apply a modifier (in your case .backgound(.red)) to a view we actually get back a new view type called ModifiedContent, which stores both our original view and its modifier. This means when we apply a modifier, the actual view that goes into the hierarchy is the modified view, not the original one. When Paul uses word background in this context he means .backround(.red) modifier and nothing ELSE! Hierarchy is as follows: ContentView -> GeomertyReader -> ModifiedContent(Text <- backround.red)
Modified content that is Text.backround(.red) knows its place and coordinates on the screen. Just try to run this and you will see coordinates in global coordinates system i.e. on the screen
And as it knows it space coordinates on the screen it can realize, ok i am touching here border of safe area, and backrounds are told to ignore that, so I will cross it :) You are just going too much into details, it will come to you soon, that you don't always need to know how everything works in so much details. Not knowing how tv set works under the hood, does not prevent you from watchin it and switching channels. Just keep coding :) I still do not understand all the details that might be happining under the hood. I doubt there is a person who knows all. |
|
@ygeras thanks! I think I understand how it works now, with your example
it must mean that SwiftUI has already positioned the GeometryReader and all its parents in global space before the TextView or Backround even decides its bounds. That actually makes sense |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
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.