跳至主要內容

SwiftUI

naijoug大约 1 分钟

reference

Combine

OpenSource Apps

UIKit -> SwiftUI

Concept

  • SwiftUI vs UIKit

    SwiftUIUIKit
    Text & LabelUILabel
    TextFieldUITextField
    TextEditorUITextView
    Button & LinkUIButton
    ImageUIImageView
    NavigationViewUINavigationController & UISplitViewController
    ToolbarItemUINavigationItem
    ScrollViewUIScrollView
    ListUITableView
    LazyVGrid & LazyHGridUICollectionView
    HStack & VStackUIStack
    LazyHStack & LazyVStackUIStack
    TabViewUITabBarController & UIPageViewController
    ToggleUISwitch
    SliderUISlider
    StepperUIStepper
    ProgressViewUIProgressView & UIActivityIndicatorView
    PickerUISegmentedControl
    DatePickerUIDatePicker
    Alert & ActionSheetUIAlertController
    MapMapKit

Usage

// SwiftUI 中使用 UIKit
//  > SwiftUI 中使用 UIView
struct SearchView: UIViewRepresentable {
    func makeUIView(context: Context) -> UISearchBar {
        let searchBar = UISearchBar()
        searchBar.placeholder = "search..."
        return searchBar
    }
    func updateUIView(_ uiView: UISearchBar, context: Context) {
        
    }
}
//  > SwiftUI 中使用 UIViewController
struct ColorPickerView: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> UIColorPickerViewController {
        let controller = UIColorPickerViewController()
        return controller
    }
    func updateUIViewController(_ uiViewController: UIColorPickerViewController, context: Context) {
        
    }
}

// UIKit 中使用 SwiftUI
UIHostingController(rootView: SearchView())