# SwiftUI 使用 UIKit 做为UI控件不足的补充

本文主要目的就是解决目前 SwiftUITextView 的不足. SwiftUIXcode Version 11.1 中的 TextView 依然有许多问题, 文本在视图内部没法正确显示是使用的当务之急,目前的解决办法是利用 UITextView进行 “ 曲线救国 ”bash

先了解Representable 协议

Representable 协议容许咱们在 Swift 中呈现 UIView, NSView, WKInterfaceObjectapp

其关系对应以下:ide

UIView UIViewRepresentable
NSView NSViewRepresentable
WKInterfaceObject WKInterfaceObjectRepresentable

一. Representable 协议有两个必须执行的方法

  • 1. Make Coordinator(可选) 初始化过程当中首先调用动画

  • 2. makeUIView(必须) 是建立你但愿在SwiftUI中呈现的视图或者控制器的地方ui

  • 3. updateUIView(必须) 该方法是把这个视图,更新到当前配置的地方spa

  • 4. Dismantle(可选) 视图消失以前调用code

在初始化过程当中首先调用 Make 方法,而后再调用 update方法, update方法能够屡次调用,只要是请求更新的时候均可以调用, 这两个方法是呈现视图仅有的两个必须方法教程


二. UIViewRepresentableContext

上面说的两个视图呈现的必须方法,在实际的项目中使用不多是简单的视图展现,更多的是动做事件或者委托,好比:接口

  1. 在SwiftUI 中读取内容,并做出响应
  2. 了解当前视图上是否有动画
  3. 等等...

为了更好的处理视图SwiftUI直接的关系,Representable协议是关键的存在;它能够在视图、视图控制器、以及界面控制器中使用。事件

Representable Context 有三个属性:

  • 1.Coordinator 用于实现常见模式,帮助你协调你的视图和 SwiftUI 包括委派,数据源和目标动做.

  • 2.Environment 帮助你读取SwiftUI的环境, 这多是系统环境,例如配色方案或尺寸类别或设备方向. 也能够是应用app自定义的环境属性.

  • 3.Transaction 让咱们的视图知道SwiftUI中是否有动画, 可表示的上下文可用于视图,视图控制器以及接口控制器.


三. UIViewRepresentableContext 的使用

案例:在 SwiftUI 视图中嵌入基于 UIKitUITextView的控件

上面咱们回顾:


一.Representable 协议有两个必须执行的方法

1. Make Coordinator(可选)
2. Make View
3. Update View
4. Dismantle View(可选)

二. Representable Context

1. Coordinator
2. Environment
3. Transaction
	
复制代码

Make ViewUpdate View 中咱们传入 Representable Context参数,因为 Representable Context 的三个属性: Coordinator Environment Transaction

若是要使用 Coordinator,则须要在可选的 Make Coordinator 方法中建立,在初始化过程当中首先调用的是 Make Coordinator方法, 咱们把 UIKit相关的动做事件或者委托在 Make Coordinator中处理好,这样关于 UIKit 相关的 动做事件或者委托的就能够传递到 SwiftUI 里面了;

咱们看例子:

struct TextView: UIViewRepresentable {
    
    class Coordinator : NSObject, UITextViewDelegate {
        var textView: TextView

        init(_ uiTextView: TextView) {
            self.textView = uiTextView
        }

        func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
            return true
        }

        func textViewDidChange(_ textView: UITextView) {
            self.textView.text = textView.text
        }
    }
    
    @Binding var text: String

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
        textView.delegate = context.coordinator
        textView.isScrollEnabled = true
        textView.isEditable = true
        textView.isUserInteractionEnabled = true
        return textView
    }

    func updateUIView(_ uiView: UITextView, context: Context) {
        uiView.text = text
    }
}
复制代码
  1. 先调用 makeCoordinator 建立 UIViewRepresentableContext用于处理 UIKit 中的动做事件或者委托;

  2. makeUIView 中添加动做事件或者委托的目标 textView.delegate = context.coordinator

四. 最后

关于如何在SwiftUI中打造使用 UITextView上面已经很详细,更多的自定义处理须要根据需求进一个扩展;可是核心点就是两点的使用:

1. UIViewRepresentable

2. UIViewRepresentableContext


本文在苹果开发者官网教程中有 SwiftUI 直接使用 UIKit 英文教程

相关文章
相关标签/搜索