SwiftUI 与 UIKit 对等项
View Controllers
| UIKit | SwiftUI | Note |
|---|---|---|
| UIViewController | View | |
| UITableViewController | List | 你也可以使用 ScrollView 和 LazyHStack 或 LazyVStack |
| UICollectionViewController | LazyVGrid and LazyHGrid | 目前没有 SwiftUI 视图替代这个,但你可以 通过组成 List 来模拟一些布局,如 组合复杂接口的 教程,在 iOS 14 中,我们现在有 LazyVGrid 和 LazyHGrid. |
| UISplitViewController | NavigationView | |
| UINavigationController | NavigationView | |
| UIPageViewController | TabView | iOS 14 中 TabView 的一种风格 |
| UITabBarController | TabView | |
| UISearchController | - | |
| UIImagePickerController | - | |
| UIVideoEditorController | - | |
| UIActivityViewController | - | |
| UIAlertController | Alert |
Views and Controls
TabView下的 的一部分
| UIKit | SwiftUI | Note |
|---|---|---|
| UILabel | Text, Label | |
| UITabBar | TabView | |
| UITabBarItem | TabView | .tabItem |
| UITextField | TextField | 对于密码 (isSecureTextEntry) 使用 SecureField |
| UITextView | TextEditor | iOS 14 |
| UITableView | List | 还有 VStack 和 Form |
| UINavigationBar | NavigationView | NavigationView |
| UINavigationItem | ToolbarItem | iOS 14 |
| UIBarButtonItem | NavigationView | .navigationBarItems 在 NavigationView中 |
| UICollectionView | LazyVGrid and LazyHGrid | iOS 14 |
| UIStackView | HStack, LazyHStack | .axis == .Horizontal |
| UIStackView | VStack, LazyVStack | .axis == .Vertical |
| UIScrollView | ScrollView | |
| UIActivityIndicatorView | ProgressView with CircularProgressViewStyle | iOS 14 |
| UIImageView | Image | |
| UIPickerView | Picker | |
| UIButton | Button, Link | |
| UIDatePicker | DatePicker | |
| UIPageControl | iOS 14. 自动添加 TabView PageTabViewStyle 样式。 你可以控制它的外观 通过 .indexViewStyle。 | |
| UIProgressView | ProgressView | iOS 14 |
| UISegmentedControl | Picker | Picker 的一种风格 (SegmentedPickerStyle) |
| UISlider | Slider | |
| UIStepper | Stepper | |
| UISwitch | Toggle | |
| UIToolBar | NavigationView with .toolbar | iOS 14 |
| MKMapView | Map | import MKMapView 使用它 |
框架集成 - UIKit 中 SwiftUI
将 SwiftUI 视图集成到现有应用中,并嵌入 UIKit 视图和控制器 进入 SwiftUI 视图层次结构。
| UIKit | SwiftUI | Note |
|---|---|---|
| UIView | UIViewRepresentable | |
| UIViewController | UIViewControllerRepresentable |
框架集成 - SwiftUI in UIKit
将 SwiftUI 视图集成到现有应用程序中,并将 UIKit 视图和控制器嵌入到 SwiftUI 视图层次结构中。
| UIKit | SwiftUI | Note |
|---|---|---|
| UIView (UIHostingController) | View | 没有直接转换成UIView,但是可以使用容器视图将视图从 UIViewController 添加到查看层次结构 |
| UIViewController (UIHostingController) | View |
纯 SwiftUI
iOS 14
iOS 14
在 iOS 14 中,您无需 UIKit 即可编写整个应用程序。 查看来自 WWDC2020 的 SwiftUI 中的应用要点会议。
| UIKit | SwiftUI | Note |
|---|---|---|
| UIApplicationDelegate | App | |
| UIWindowSceneDelegate |