SwiftUI全攻略——1.SwiftUI和UIKit交互

1.如何将一个ViewController转换为SwiftUI的View,引入到其他SwiftUI文件?

定义一个结构体遵守UIViewControllerRepresentable协议,然后实现里面的makeUIViewControllerupdateUIViewController方法,在make里返回需要转换的ViewController

struct ContactsListRepresentation: UIViewControllerRepresentable {
    
    
    func makeUIViewController(context: Context) -> ContactsListViewController {
        ContactsListViewController()
    }
    
    func updateUIViewController(_ tableViewController: ContactsListViewController, context: Context) {

    }
}

struct ContractsListViewControllerPreviews: PreviewProvider {
    static var previews: some View {
        ContactsListRepresentation()
    }
}

这样就将ContactsListViewController转换成为ContactsListRepresentation,后者属于SwiftUI的View类型,可以直接引入到SwiftUI中了

import SwiftUI

struct TestSwiftUI: View {
    var body: some View {
        ContactsListRepresentation()
    }
}

struct TestSwiftUI_Previews: PreviewProvider {
    static var previews: some View {
        TestSwiftUI()
    }
}

2.如何将一个UIView转换为SwiftUI里面的View,并且引入到其他SwfitUI文件?

曾经使用UIKit自定义的UIView控件,如何引入SwiftUI呢,其实也很简单,只需要定义一个新的结构体,遵守UIViewRepresentable协议

struct ContactCellRepresentable: UIViewRepresentable {
    
    
    func makeUIView(context: Context) -> ContactCell {
        
        let cell = Bundle.main.loadNibNamed("ContactCell", owner: self, options: nil)?.first as! ContactCell
        
        return cell
    }
    
    
    func updateUIView(_ uiView: UIViewType, context: Context) {
        
    }
    
    
}
struct ContactCellPreviews: PreviewProvider {
    static var previews: some View {
        ContactCellRepresentable()
    }
}

之后就可以直接在SwiftUI文件中引入ContactCell,他的名字变成了ContactCellRepresentable

struct TestUIViewConvertToSwiftUI: View {
    var body: some View {
        ContactCellRepresentable()
    }
}


struct TestUIViewConvertToSwiftUI_Previews: PreviewProvider {
    static var previews: some View {
        TestUIViewConvertToSwiftUI()
    }
}

3.如何将一个SwfitUI的View转换为一个ViewController

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let contact = ContactInfomation.generateData().first!
        let profileView = ProfileView(contact: contact)
        let profileViewController = UIHostingController(rootView: profileView)
        
        profileViewController.view.frame = view.bounds
        addChild(profileViewController)
        profileViewController.didMove(toParent: self)
        view.addSubview(profileViewController.view)  
    }
}

只需要生成View,并且设置为UIHostingControllerrootView,就可以将SwiftUI包装成一个ViewContoller
此处的测试需要将SceneDelegate里的入口注释掉

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
//        if let windowScene = scene as? UIWindowScene {
//          let window = UIWindow(windowScene: windowScene)
//            let rootView = TestSwiftUIVIewConvertToUIView()
//            window.rootViewController = UIHostingController(rootView: rootView)
//          self.window = window
//          window.makeKeyAndVisible()
//        }
//        guard let _ = (scene as? UIWindowScene) else { return }
    }

The Best Way to Learn is to Teach

github仓库地址 https://github.com/SunZhiC/LearningSwiftUI

你可能感兴趣的:(SwiftUI全攻略——1.SwiftUI和UIKit交互)