SwiftUI和UIKit簡單融合

一:將UIKit 中UIViewController融入SwiftUI

建立中轉(zhuǎn)層

1),創(chuàng)建SwiftUI文件,引入UIViewControllerRepresentable協(xié)議

import SwiftUI
import UIKit
struct MainViewUI<View>: UIViewControllerRepresentable{
}

2),設(shè)置controller類型,一般都是繼承UIViewController

import SwiftUI
import UIKit
struct MainViewUI<View>: UIViewControllerRepresentable{

       typealias UIViewControllerType = UIViewController

}   

3),實現(xiàn)兩個協(xié)議
一:使UIKit的controller映射到SwiftUI
二:controller更新后需要做的操作

import SwiftUI
import UIKit
struct MainViewUI<View>: UIViewControllerRepresentable{

func makeUIViewController(context: Context) -> UIViewController {
    let VC = UIKitVC()
    return VC
}

func updateUIViewController(_ vc: UIViewController, context: Context) {
    
   
}

typealias UIViewControllerType = UIViewController

}

使用

NavigationLink("Show UIKit VC", destination: MainViewUI<Any>())

注:UIKit的controller代碼,即UIKitVC

import UIKit
class UIKitVC: UIViewController ,UITableViewDelegate,UITableViewDataSource{

override func viewDidLoad() {
    super.viewDidLoad()
    
    let label = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: 200, height: 30))
    label.text = "這是UIKit中的視圖"
    self.view.addSubview(label)
    //頂部88,底部83
    let tabView = UITableView.init(frame: CGRect.init(x: 0, y: 30, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height-230))
    self.view.addSubview(tabView)
    tabView.delegate = self
    tabView.dataSource = self
    
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    if cell == nil {
        cell = UITableViewCell.init(style: .subtitle, reuseIdentifier: "cell")
    }
    cell?.textLabel?.text = "標(biāo)題"
    cell?.detailTextLabel?.text = "小標(biāo)題"
    cell?.imageView?.image = UIImage.init(named: "menu")
    return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}

}
截屏2021-02-12 15.47.47.png

二:使用UIKit中的view

大致方式和Controller相同,只是引入?yún)f(xié)議不同,此處引入UIViewRepresentable協(xié)議

建立中轉(zhuǎn)層

import SwiftUI

struct ShowUIKitView<View> :UIViewRepresentable{
func makeUIView(context: Context) -> UIView {
    let contentView = UIKitView.init(frame: CGRect.init(x: 50, y: 50, width: 200, height: 200))
    return contentView
}

func updateUIView(_ uiView: UIView, context: Context) {
    
}

typealias UIViewType = UIView


}

使用方式

在SwiftUI中調(diào)用

ShowUIKitView<Any>()
        .padding(.leading,20)
        .padding(.top,50)

其余當(dāng)作SwiftUI控件正常使用

注:UIKit中view的代碼

import UIKit

class UIKitView: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)
    
    let imgV = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: 50, height: 50))
    imgV.image = UIImage.init(named: "menu")
    self.addSubview(imgV)
    imgV.layer.cornerRadius = 6
    imgV.layer.masksToBounds = true
    let label = UILabel.init(frame: CGRect.init(x: 50, y: 0, width: 100, height: 60))
    label.numberOfLines = 0
    label.text = "這是UIKit中的視圖"
    self.addSubview(label)
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}
截屏2021-02-12 15.59.49.png

三:UIKit使用SwiftUI

UIHostingController承載SwiftUI中的view

    let vc = MainUIView.init()

    let swiftUIVC = UIHostingController.init(rootView: vc)

    swiftUIVC.hidesBottomBarWhenPushed = true
    
    self.navigationController?.pushViewController(swiftUIVC, animated: true)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容