繁体   English   中英

如何在 SwiftUI 视图中使用自定义 UI 控件,例如 Button?

[英]How to use custom UI controls like Button in SwiftUI view?

我们为我们的应用程序设计了一些自定义控件,例如 CustomButton、CustomTexView 等。 这些控件定义了应用程序的主题并具有标准尺寸。 现在我们计划在我们的项目中使用 SwiftUI。 如何在 SwiftUI 结构中使用这些自定义控件?

IE

struct ContentView: View {
  var body: some View {
     HStack {
        Text("SwiftUI from ContentView2")
        Rectangle() //Here we need to add custom button. i.e. CustomButton defined in our project which is written in UIKit`enter code here`
     }
 }

}

这段代码属于这个问题,原始海报有很多信息。 这不是它的答案,但它是如何将现有 Storyboard 转换为 SwiftUI View的一个很好的例子

import SwiftUI

struct ContentView: View {
var body: some View {
    StoryboardViewController()
}
}

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

struct StoryboardViewController: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> some UIViewController {
    let storyboard = UIStoryboard(name: "Storyboard", bundle: Bundle.main)
    let controller = storyboard.instantiateViewController(identifier: "Main")
    return controller
}

func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
    
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM