|
I'm fiddling with a view layout - a graph - where I'm seeing how far I can get within an all-SwiftUI layout. Each of the component pieces are fine, but assembling the whole isn't working quite as I'd like. What I've found is that I can easily constrain sizing along a single stack axis - but not two axis at once: (both vertical and horizontal). I started to reach for AlignmentGuides, as I noticed you can align non-siblings with a custom guide. That will help my goal, but it doesn't solve the sizing part, which is the heart of this question: Is there a way to constrain a view's size based on another, non-sibling, view? A simplification of the structure is:
Which maps out to:
Is there a way to tell CellFour (which isn't in the same HStack as cell's 1 and 2) that I want it to constrain itself (and align) to the width of cell CellTwo? This does not need to strictly be a grid view (per https://www.hackingwithswift.com/quick-start/swiftui/how-to-position-views-in-a-grid) - there are really only three views that I care about in this case. The areas that roughly map to cell 1, cell 2, and cell 4. I want the heights of Cell 1 and Cell 2 to be the same (accomplished easily with the current (I've also inquired on StackOverflow) |
|
Since your cells 2 and 4 are not part of the same view hierarchy, you would have to use PreferenceKeys to push information from one of them up the chain and then down the chain to the other one. Two great resources for learning about this method are: |
|
Here is my quick attempt to use the PreferenceKey protocol to push the cell frame info up the view hierarchy.
|
|
Using the preference/environment value propagation technique I went with this solution that can size multiple columns at once :
I've also included the part that propagates the height among the views —although less useful. You can test it with this preview :
Too bad we can't attach image, you won't see the result without running it yourself. PS: If someone have a better idea regarding the components' names it'll be much appreciated :) |
SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.