ios - 是否可以将 ViewController 分配给 SwiftUI View ?

标签 ios swift swiftui uiviewcontroller uikit

我正在使用 SwiftUI 应用程序协议(protocol)构建 SwiftUI 应用程序。

我需要访问一些 UIKIt 函数,特别是控制 NavigationView 的函数,如下所示:https://stackoverflow.com/a/56555928/13727105 .

显然,要做到这一点,我需要将 SwiftUI View 与 ViewController 绑定(bind)。

我尝试执行以下操作:

ContentView.swift

struct ContentView: View {
    
    var body: some View {
        ZStack {
            ContentViewIntegratedController() // <- here
            NavigationView{
                ScrollView {
                    Text("Content View")
                        .navigationTitle("Content View")

                }
            }
        }
    }
}

class ContentViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

struct ContentViewIntegratedController: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> some UIViewController {
        return ContentViewController()
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType,
                                context: UIViewControllerRepresentableContext<ContentViewIntegratedController>) {}
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


但调用 ContentViewIntegratedContoller(第 5 行)似乎会创建一个新预览,而不是修改现有预览。

有没有办法集成 ViewController 来修改 SwiftUI View ?

我不想做什么:

  • 创建一个包含 SwiftUI View 的 Storyboards 应用
  • ContentViewIntegratedViewController 创建 Storyboards View 。

是否有其他方法可以访问这些功能,而无需将 ViewController 添加到我的 SwiftUI View ?

TIA。

最佳答案

    struct ContentView: View {
    var body: some View {
        ContentViewIntegratedController(view: YourView())
            .ignoresSafeArea()
    }
    }




--------------------------------------------------------------
    struct YourView: View {
    var body: some View {
        ScrollView{
        Text("Hello, World!")
        }
    }}
------------------------------------------------------
    import Foundation
    import SwiftUI

    struct ContentViewIntegratedController :UIViewControllerRepresentable{
    
    var view: YourView
    
    init(view:YourView) {
        self.view = view
    }
    
    func makeUIViewController(context: Context) -> UINavigationController{
        
        let childView = UIHostingController(rootView: view)
        let controller =     UINavigationController(rootViewController:childView)
        let appearance = UINavigationBarAppearance()
        let searchController = UISearchController()
        
        
        searchController.searchBar.barStyle = .black
        
        appearance.backgroundColor = UIColor(Color(.red))
        appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
        appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
        
        
        controller.navigationBar.topItem?.compactAppearance = appearance
        controller.navigationBar.topItem?.scrollEdgeAppearance = appearance
        controller.navigationBar.topItem?.standardAppearance = appearance
        

        controller.navigationBar.topItem?.title = "navigation bar"
        controller.navigationBar.prefersLargeTitles = true
        
        searchController.searchBar.searchTextField.attributedPlaceholder = NSAttributedString(string: "Rechercher...", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
        searchController.searchBar.setValue("Annuler", forKey: "cancelButtonText")
        
        
        searchController.searchBar.showsBookmarkButton = true
        searchController.searchBar.searchTextField.leftView?.tintColor = .white
        
        let sfConfiguration = UIImage.SymbolConfiguration(pointSize: 30)
        let barCodeIcon = UIImage(systemName: "barcode.viewfinder")?.withTintColor(.white, renderingMode: .alwaysOriginal).withConfiguration(sfConfiguration)
    

        searchController.searchBar.setImage(barCodeIcon, for: .bookmark, state:.normal)
        searchController.obscuresBackgroundDuringPresentation = false
  

        let attributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
        UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
       
        controller.navigationBar.topItem?.hidesSearchBarWhenScrolling = false
        controller.navigationBar.topItem?.searchController = searchController
        
        return controller
        
    }
    
    
    func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {
        
    }}

Example

关于ios - 是否可以将 ViewController 分配给 SwiftUI View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69219001/

相关文章:

iphone - 如何使用 UIGestureRecognizer 子类实现滑动

ios - 空白应用程序无法调试构建 -/找不到 Info.plist 文件

ios - 在 Swift 中访问类函数内的函数

ios - SwiftUI 在多平台中获取屏幕大小

ios - 无论文本长度如何,如何使 SwiftUI TextView 显示两行?

c# - WebAPI 无法解析 multipart/form-data post

ios - 在 UITextView.text 中确定单词并使用 Swift 2.2、iOS 9.3 和 Xcode 7 修改带有前缀的单词的方法?

swift - "Correct"快速的 Realm 模型?

ios - UIViewController 部分标题未显示

ios - SwiftUI: ScrollView 中的顶部锚定可调整大小的 View