How to implement flexLayout with SwiftUI ? Or re-customize the layout of SwiftUI ?

I try to do it, but some properties can not imp.
This project is a new cross-platform solution, but it is hindered by the inflexible layout of SwiftUI. Can anyone provide better ideas.

I suspect this is not possible in general case. Not until Apple opens some of the private layout API.

Implementing flex-grow and flex-shrink requires you to get the ideal size of the view while final computed size of the view may be different from both the ideal size and constraints.

You may try to construct some statefull monstrosity where you first have a state where you let subviews resize themselves to their ideal size according to constraints, record that size and then switch to a different state when you apply final computed layout.

Note that ideal size is not some specific value, it is a function of constraints. You may need to go through multiple measument states.

If you need to support nested flex layouts, your parent layout need to wait until child layout is done measuring its children and is ready to provide its ideal size. You may use preferences to communicate this up the hierarchy.

And also you need to be notified when ideal size changes. You can use environment to inject some kind of observer, but note that built-in won’t notify it. This can work only if you control every view in the hierarchy.

And if all of this hackery works, it will not have a linear time complexity. Probably quadratic or exponential in total number of views.

And I suspect multi-state approach may have issues with animation.

In the summary, UIKit is probably a more suitable backend for you. Or consider using simpler layout system, which is naturally a subset of SwiftUI layout.

tks. I agree with your point. I did encounter problems with the properties of flex-grow and flex-shrink, but I cannot use UIKit because it is not only a syntax statement, but also contains rendering information.

Hi, @erwinan. Apple has asked that questions that are just about how to use Apple frameworks like SwiftUI should be asked on the Apple developer forums.

Please read the question carefully.

SwiftUI is an Apple framework (like UIKit), despite its name. It's not part of Swift.

Terms of Service

Privacy Policy

Cookie Policy