gordontucker / FittedSheets

Bottom sheets for iOS
MIT License
1.32k stars 278 forks source link

SwiftUI is unresponsive #211

Closed andreymosin closed 1 year ago

andreymosin commented 1 year ago

I'm having an issue when SwiftUI views are unresponsive in Intrinsic Size mode. However if I have pulling up enabled and drag the sheet a bit up, it becomes responsive and buttons work. Doesn't occur on iOS 17 tho Thank you!

Code is simple:

        let vc = UIHostingController(rootView: SwiftUIView())
        let options = SheetOptions(shrinkPresentingViewController: false)
        parent.present(SheetViewController(controller: vc, options: options), animated: true)
thedemonswithin commented 1 year ago

@andreymosin I've found fix for this issue. Each sheet view should contain var sheetResizeAction: (() -> Void)? that will be called after view size pref key changed:

VStack {

}
.readSize(onChange: { _ in
    sheetResizeAction?()
})

This action should only update intrinsic view height

***
let sheetController = SheetViewController(controller: hostingController, sizes: getSheetViewSizes(), options: getSheetViewOptions())
view.sheetResizeAction = {
    sheetController.updateIntrinsicHeight()
}
***
import SwiftUI

public struct SizePreferenceKey: PreferenceKey {
    public typealias Value = CGSize
    public static var defaultValue = CGSize.zero

    public static func reduce(value: inout CGSize, nextValue: () -> CGSize) {
        value = nextValue()
    }
}

public struct ViewGeometry: View {
    public init(){}
    public var body: some View {
        GeometryReader { proxy in
            Color.clear
                .preference(key: SizePreferenceKey.self,
                            value: proxy.size)
        }
    }
}

public extension View {
    func readSize(onChange: @escaping (CGSize) -> Void) -> some View {
        overlay(ViewGeometry())
            .onPreferenceChange(SizePreferenceKey.self, perform: onChange)
    }
}
andreymosin commented 1 year ago

This seems to have solved the issue, thank you