SwiftUI UIViewControllerRepresentable.updateUIViewController(_:context:)の流れ

UIViewControllerRepresentable.updateUIViewController(_:context:)の流れ

Example

RefillView

import SwiftUI

struct RefillView: View {
    let screenSize: CGSize = UIScreen.main.bounds.size
    @State var onSettingView: Bool = false

    var body: some View {
        ZStack {
            Image("testtemplate")
                .resizable()
                .scaledToFit()
                .frame(width: screenSize.width, height: screenSize.height, alignment: .center)
            PencilKitViewControllerView(onSettingView: self.$onSettingView)
            VStack {
                HStack {
                    Button(action: {
                        print("menu tap")
                        self.onSettingView.toggle()
                    }, label: {
                        Image(systemName: "square.grid.3x3")
                            .font(.largeTitle)
                    })
                    Spacer()
                }
                Spacer()
            }
            .padding(8.0)
        }
    }
}

struct RefillView_Previews: PreviewProvider {
    static var previews: some View {
        RefillView()
    }
}

PencilKitViewControllerView

import SwiftUI
import PencilKit

struct PencilKitViewControllerView: UIViewControllerRepresentable {
    @Binding var onSettingView: Bool
    
    func makeUIViewController(context: Context) -> PencilKitViewController {
        return PencilKitViewController()
    }

    func updateUIViewController(_ controller: PencilKitViewController, context: Context) {
        print(#function)
        controller.setOnSettingCalendarView(on: self.onSettingView)
    }
}

SwiftSwift,SwiftUI

Posted by shi-n