Layout question

Hallo and nice to meet you all.

I'm a newbie to Swift and struggle with some (probably) simple concepts that I can't figure out.

I'm having a layout with 3 areas, 1 Left, 1 Right and below both 1 Button bar. Something like this:


The button bar contains multiple buttons that will show different views in the Left area.

I started with a View (ContentView) that contains 3 subviews Left1(), Right() and ButtonBar() nicely positioned with VStack and HStack.

In my mind I think it can't be too difficult to load a different view like Left2() in the left area (Where currently Left1() is show), but for some reason I can get this to work.

Any thoughts?

PS Working with SwiftUI

Much appreciated

Hey @falco - since this is about SwiftUI, it's a tad off topic for the public forums, since it's a private Apple framework - so obligatory noises provided. That said, the issue isn't likely with layout, but I'd guess more of your understanding about how SwiftUI updates itself.

SwiftUI is declarative, so (unlike UIKIt or AppKit) within the view itself you're not "changing the view". Instead, I'd recommend thinking about it like "When the view is created, what do I want to display - and how do I determine that?". The idea being to lean into the idea that the view is a reflection of some state that's passed into the view. Then when you change the state, the SwiftUI recognizes the state changing and re-creates the view based on your updated state.

I don't know your specific use case, but it could be as simple as having some local boolean and then setting up your view with an if statement, displaying Left1() when the state is one value, and Left2() when the state is another value.

If you haven't watched them yet, definitely run through SwiftUI Essentials - WWDC19 - Videos - Apple Developer and later Demystify SwiftUI - WWDC21 - Videos - Apple Developer. The second of those goes into a lot of detail about how to think about creating your views for SwiftUI.

Hi @Joseph_Heck ,

First thank you for pointing out the fact this forum is not supporting SwiftUI. Did not know Swift and SwiftUI are from different backgrounds, guess I'm a real newbie.

Based on your description I think I'm not that far off the track...

Guess my problem is related to setting the local boolean from a different view (how the variable is setup)

Thanks again, I'll defiantly watch the videos

gr Falco