Search code examples
iosswiftuigeometryreader

iOS SwiftUI - GeometryReader on size / height change


I have this so far:

                .background(
                    GeometryReader { geo in
                        Color.clear
                            .onAppear {
                                profileViewHeight = geo.size.height
                            }
                    }
                )

I need to update profileViewHeight also as soon as the height of this view changes.

I tried:

                .background(
                    GeometryReader { geo in
                        Color.clear
                            .onAppear {
                                profileViewHeight = geo.size.height
                            }
                            .onChange(of: , perform: {
                                profileViewHeight = geo.size.height
                            })
                    }
                )

But I don't know what is expected for of

I'm new to ios/swiftui and don't know how exactly to use this


Solution

  • I solved it this way:

        .background(
            GeometryReader { geo in
                Color.clear
                    .onAppear {
                        profileViewHeight = geo.size.height
                    }
    
                    .onChange(of: geo.size.height) { _ in
                        if(geo.size.height > profileViewHeight){
                            profileViewHeight = geo.size.height
                        }
                    }
                
            }
        )