UIController转为SwiftUI

在UIKit转到SwiftUI的过渡时期中,项目中会遇到不得不用到二者混合使用的情景,苹果这时提供了相关API让iOSer更好地适应这个时期。

UIViewControllerRepresentable协议

此协议属于SwiftUI,新建一个实现此协议的结构体,在其中传入UIViewController,此结构体可以在SwiftUI中使用。

import SwiftUI

//struct SwiftUIView: UIView {
struct SwiftUIView: UIViewControllerRepresentable {
   
    // MARK: UIViewControllerRepresentable
    typealias UIViewControllerType = MrYu4ViewController

    func makeUIViewController(context: Self.Context) -> Self.UIViewControllerType {
        let vc = MrYu4ViewController()
        return vc
    }

    func updateUIViewController(_ uiViewController: Self.UIViewControllerType, context: Self.Context) {

    }
}

SwiftUIConvert

posted @ 2022-09-29 21:45  MrYu4  阅读(99)  评论(0编辑  收藏  举报