工作生活SwiftUI

SwiftUI(二) segmentWithWebViews

2019-06-30  本文已影响0人  白色天空729

import SwiftUI
import WebKit

struct ContentView : View {
    
    @State private var selectedSegment = 0
    
    @State private var websites = ["www.jianshu.com/p/72cccb22a652","www.baidu.com"]
    
    var body: some View {
        NavigationView {
            VStack {
                
                HStack {
                    SegmentedControl(selection: $selectedSegment) {
                        ForEach(0 ..< websites.count) {
                            Text(self.websites[$0]).tag($0)
                        }
                    }
                }
                WebView(request: URLRequest(url: URL(string: "https://\(websites[selectedSegment])")!))
                
                
            }.navigationBarTitle(Text("网页"))
        }
    }
}

struct WebView : UIViewRepresentable {
    
    let request : URLRequest
    
    func makeUIView(context: Context) -> WKWebView {
        return WKWebView()
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
        uiView.load(request)
    }
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

UIViewRepresentable
A view that represents a UIKit view.
代表一个UIKit的视图。

效果如下:


webView.gif
上一篇下一篇

猜你喜欢

热点阅读