美文网首页工作生活SwiftUI
SwiftUI(二) segmentWithWebViews

SwiftUI(二) segmentWithWebViews

作者: 白色天空729 | 来源:发表于2019-06-30 11:41 被阅读0次
    
    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

    相关文章

      网友评论

        本文标题:SwiftUI(二) segmentWithWebViews

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