LiYanan2004

在 SwiftUI 中添加 UIScrollView 中有,而 ScrollView 中没有的功能

兼容模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import SwiftUI

struct LegacyScrollView<Content: View>: UIViewRepresentable {

// any data state, if needed

var content: () -> Content

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

init(@ViewBuilder content: @escaping () -> Content) { self.content = content }

func makeUIView(context: Context) -> UIScrollView {
let control = UIScrollView()
control.refreshControl = UIRefreshControl()
control.refreshControl?.addTarget(context.coordinator, action: #selector(Coordinator.handleRefreshControl), for: .valueChanged)
let contentView = UIHostingController(rootView: content()).view!
contentView.frame = UIWindow().frame
control.addSubview(contentView)
return control
}

func updateUIView(_ viewController: UIScrollView, context: Context) {
// code to update scroll view from view state, if needed
}

class Coordinator: NSObject {
var control: LegacyScrollView

init(_ control: LegacyScrollView) {
self.control = control
}

@objc func handleRefreshControl(sender: UIRefreshControl) {
// Start Refreshing
sender.endRefreshing()
}
}
}

使用方法

1
2
3
LegacyScrollView {
// Your Views Here ( Written in Swift UI )
}
Enjoy it ~

 海报

 评论


字数统计:25k  |   正在计算...
总访问量为 次 。