Home > Software engineering >  SwiftUI 4: How to solve AnyLayout 'init(_:)' error
SwiftUI 4: How to solve AnyLayout 'init(_:)' error

Time:07-28

I'm trying to change from one Layout to another when Dynamic Type Size changes. I've tried enter image description here

Sample code:

struct DynamicLayoutExample: View {
    @Environment(\.dynamicTypeSize) var dynamicTypeSize

    var body: some View {
        let layout = dynamicTypeSize <= .medium ?
            AnyLayout(HStack()) : AnyLayout(VStack())

        layout {
            Text("First label")
            Text("Second label")
        }
    }
} 

CodePudding user response:

As a work around you can use _HStackLayout() and _VStackLayout() if you want to see it working.

There is a fantastic post on enter image description here

Tested on Xcode 14 Beta 3 in iOS 16 simulator

  • Related