TIL: ViewThatFits

Since Apple didn't bring backward compatibility to SwiftUI, I have a love-hate relationship with it. Lately, I've been catching up on what's new in iOS 16 and realized this little guy called ViewThatFits. It is a generic view that takes multiple views and returns the appropriate one size-wise. Here is how to use it 👇

ViewThatFits(.horizontal) {
    SomeView()
        .frame(width: 700, height: 75)

    AnotherView()
        .frame(width: 350, height: 75)

    Text("None of the above fits!")
}

If you are in portrait mode, AnotherView() will show up since it has a smaller width and vice versa you got the point. You might say this can be achieved with conditional statements but it brings unnecessary nesting. Hope that it will not become another thing that is deprecated after another WWDC 🤞