

SwiftUI Integrate with UIKit Tutorial
source link: https://www.tuicool.com/articles/b2EVN3F
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

At the current state of SwiftUI not all views are implemented yet. UIKit views can be added in the Swift hierarchy. In this tutorial a url will be displayed in a web view.SwiftUI requires Xcode 11 and MacOS Catalina, for which the Betas can be downloaded at the Apple developer portal.
Open Xcode and either click Create a new Xcode project in Xcode’s startup window, or choose File > New > Project . In the template selector, select iOS as the platform, select the Single View App template, and then click Next . Enter SwiftUIIntegrateUIKitTutorial as the Product Name, select the Use SwiftUI checkbox, and click Next . Choose a location to save the project on your Mac.

A new webView needs to be created inside the project. Choose File -> New -> File and select iOS -> User Interface -> SwiftUI View. Name the file WebView.swift .
In the canvas, click Resume to display the preview. If the canvas isn’t visible, select Editor > Editor and Canvas to show it.

In the Project navigator, click to select WebView.swift . Change the code to
import SwiftUI import WebKit // 1. struct WebView : UIViewRepresentable { let request: URLRequest // 2. func makeUIView(context: Context) -> WKWebView { return WKWebView() } // 3. func updateUIView(_ uiView: WKWebView, context: Context) { uiView.load(request) } } #if DEBUG struct WebView_Previews : PreviewProvider { static var previews: some View { // 4. WebView(request: URLRequest(url: URL(string: "https://ioscreator.com")!)) } } #endif
-
The UIViewRepresentable protocol enabling an existing UIKit View to be added to a SwiftUI hierarchy
-
The makeUIView(context:) method is required and creates the web view to be presented.
-
The updateUI(context:) method is required and updated the presented web view to the latest configuration.
-
The preview will display the url embedded in an url request
Go to the SceneDelegate.swift file and change the window.rootViewController = … line to
window.rootViewController = UIHostingController(rootView: WebView(request: URLRequest(url: URL(string: "https://ioscreator.com")!)))
Go to the preview pane and switch to live view. The preview will look like this.

The source code of the SwiftUIIntegrateUIKitTutorial can be downloaded at the ioscreator repository on Github .
Recommend
-
28
A few weeks ago, we talked about building views like PagerView and BottomSheetView from scratch in SwiftUI . SwiftUI is pretty young and misses some components that we expect to hav...
-
20
咱们最有意思的第四篇 SwiftUI 教程来啦!为什么说是“最有意思”的呢?因为按照约定,在这篇文章里我们会一起来看看用 SwiftUI 开发界面的快捷便利体现在什么地方。相信这会让许多苹果开发者们耳目一新。 信了苹果教之后,每次有什么更新,...
-
10
在上一篇文章中,我们了解了 SwiftUI 的 Text 组件,并通过 Stack 系列的组件对内容进行了一些简单的布局。在这篇文章里,我们会认识一个全新的图片组件...
-
9
UIKit or SwiftUI: what to use in production? 2020, Sep 29 Appl...
-
10
Integrating UIKit & SwiftUIGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupShapeGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGrou...
-
16
Introspect for SwiftUI Introspect allows you to get the underlying UIKit or AppKit element of a SwiftUI view. For instance, with Introspect you can access UITableView to modify separator...
-
12
Replicate 12 UIKit's contentMode options in SwiftUI 12 Apr 2021 ⋅ 4 min read ⋅ SwiftUI
-
8
How to use SwiftUI in UIKit Using SwiftUI as UIView and UIViewController Table of ContentsIn the previous post (How to use UIKit in SwiftUI)...
-
3
访问 SwiftUI 内部的 UIKit 组件 Original...
-
9
How to Use UIHostingConfiguration to Integrate SwiftUI Views into UIKit Apps Since the release of SwiftUI in 2019, Apple has been striving to enhance the interoperability between SwiftUI and UIKit. In iOS 16,...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK