美文网首页SwiftUI
SwiftUI-混合开发

SwiftUI-混合开发

作者: YungFan | 来源:发表于2025-05-08 17:00 被阅读0次

介绍

混合开发主要是指在 SwiftUI 中使用 UIKit(SwiftUI 中使用 UIView 与 UIViewController)与在 UIKit 中使用 SwiftUI。通过混合开发,开发者可以更灵活地利用 SwiftUI 与 UIKit 的各自优势,开发出功能强大且具有良好用户体验的应用程序。

UIKit in SwiftUI

Apple 针对 UIView 与 UIViewController 提供了两个 “表示器”,如下表所示。通过这两个表示器可以很容易地将它们转换成 SwiftUI 里面的 View。

UIKit SwiftUI
UIView UIViewRepresentable
UIViewController UIViewControllerRepresentable

UIView in SwiftUI

UIViewRepresentable

  • 要使 UIView 在 SwiftUI 中可用,需要用UIViewRepresentable对 UIView 进行包装。
  • UIViewRepresentable中主要有 2 个方法需要实现。
    • makeUIView():创建View
    • updateUIView():根据条件与业务逻辑设置View的状态。

案例

使用 UIKit 中的UIActivityIndicatorView

import SwiftUI
import UIKit

struct ActivityIndicator: UIViewRepresentable {
    var isAnimating: Bool
    
    // 如下的2个方法都是与UIKit相关
    func makeUIView(context: Context) -> UIActivityIndicatorView {
        let v = UIActivityIndicatorView()
        v.color = .orange
        return v
    }
    
    func updateUIView(_ uiView: UIActivityIndicatorView, context: Context) {
        if isAnimating {
            uiView.startAnimating()
        } else {
            uiView.stopAnimating()
        }
    }
}

struct ContentView: View {    
    var isAnimating = true  
      
    var body: some View {
        ActivityIndicator(isAnimating: isAnimating)
    }
}

UIViewController in SwiftUI

UIViewControllerRepresentable

  • 要使 UIViewController 在 SwiftUI 中可用,需要用UIViewControllerRepresentable对 UIViewController 进行包装。
  • UIViewControllerRepresentable中主要有 2 个方法需要实现。
    • makeUIViewController():创建UIViewController
    • updateUIViewController():根据条件与业务逻辑设置UIViewController的状态。

案例

使用 UIKit 中的UINavigationController

import SwiftUI
import UIKit

struct NavigationViewController: UIViewControllerRepresentable {    
    var vc: UIViewController
    var title: String

    func makeUIViewController(context: Context) -> UINavigationController {       
        let nvc = UINavigationController(rootViewController: vc)  
        return nvc
    }

    func updateUIViewController(_ navigationController: UINavigationController, context: Context) {         
        navigationController.viewControllers[0].title = title        
    }
}


struct ContentView: View {    
    var body: some View {        
        NavigationViewController(vc: UIViewController(), title: "UIViewControllerRepresentable")
    }
}

SwiftUI in UIKit

UIKit 中使用 SwiftUI,需要通过UIHostingController包装 View,然后才能使用。

// 可以是复杂的ContentView
let vc = UIHostingController(rootView: ContentView())
// 也可以是简单的Text等其他View
let vc = UIHostingController(rootView: Text("Hello SwiftUI"))

相关文章

  • SwiftUI-混合开发

    在目前阶段,SwiftUI 很难独立开发一款功能强大的 App,还是需要与 UIKit 一起合作,借助 UIKit...

  • SwiftUI-开发iOS项目

    创建项目 项目文件 AppDelegate.swift — 它负责App的启动与终止,并负责与SceneDeleg...

  • 混合开发

    来源于:IOS-Hybrid(混合开发) 那些设计思想及逻辑部署

  • 混合开发

    http://ask.dcloud.net.cn/docs/组件:http://dev.dcloud.net.cn...

  • 混合开发

    混合开发 框架对比 https://www.jianshu.com/p/8e99b4aed464 混合开发-最全常...

  • 混合开发

    概述 随着移动开发的发展,互联网公司也是层出不穷,有些公司迫于竞争,想要更迅速的更省成本的进行开发,就不再满足 A...

  • Flutter与已有iOS工程混合开发

    Flutter与已有iOS工程混合开发 混合开发参考:Add-Flutter-to-existing-apps本文...

  • 一篇彻底搞懂----混合移动App干货

    一 - 混合移动App干货—详细解读 引言 本文会详细的讲解什么是混合App开发、混合App开发概念、原理、区别、...

  • 新鲜热乎的2017年Hyper App开发姿势

    APICloud混合APP开发入门指引 技术选型 APICloud 混合App开发技术之一 Bootstrap 4...

  • Flutter 与 Native 混合开发

    前言 本文主要介绍 Flutter 与 Android 的混合开发,关于 Flutter 与 iOS 的混合开发后...

网友评论

    本文标题:SwiftUI-混合开发

    本文链接:https://www.haomeiwen.com/subject/fnerijtx.html