【问题标题】:How to use custom UI controls like Button in SwiftUI view?如何在 SwiftUI 视图中使用自定义 UI 控件,例如 Button?
【发布时间】:2021-03-17 12:33:07
【问题描述】:

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

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`
     }
 }

}

【问题讨论】:

  • 自定义组件的定义看起来如何?您也可以将这些添加到问题中吗?
  • 我们已经围绕 Apple 提供的一些控件定义了包装器,例如 UIButton 具有包装器 MyButton 它在内部定义属性和颜色,例如主要、次要等。主要按钮有 44px 高度和蓝色,次要按钮有白色颜色等。这有助于我们保持跨应用程序的一致性。由于公司政策无法在此处粘贴确切的代码。

标签: ios swift swiftui uikit


【解决方案1】:

此代码属于this question 原发帖者有很多信息。这不是答案,但它是如何将现有 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) {
    
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    相关资源
    最近更新 更多