2019年8月26日
用SWiftUI的ScrollView设计界面,发现该控件没有实现Delegate系列回掉接口,因此需要将UIKit中的UIScrollView继承到SwiftUI的View上面来显示,最简单的转化代码如下所示:
import SwiftUI
import UIKit
// in swiftui view file, use like this, it will contruct a scroll view
//LegacyScrollView()
struct LegacyScrollView : UIViewRepresentable {
// any data state, like @State/@Binding/etc, if needed
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> UIScrollView {
let control = UIScrollView()
control.refreshControl = UIRefreshControl()
control.refreshControl?.addTarget(context.coordinator, action:
#selector(Coordinator.handleRefreshControl),
for: .valueChanged)
control.delegate = context.coordinator
// Simply to give some content to see in the app
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, hei