【问题标题】:How to add a TextView to the top of a VStack in swift Xcode?如何在 swift Xcode 中将 TextView 添加到 VStack 的顶部?
【发布时间】:2021-11-02 12:40:32
【问题描述】:

我想在我的应用程序的文本字段上放置一个文本视图。我在代码中将 UITextView 添加到 "Text("Hey") 时遇到了一些麻烦。当我尝试这样做时,我收到以下错误消息:

  1. 类型'UITextView.Type'不能符合'View';只有结构/枚举/类类型可以符合协议

  2. 静态方法'buildBlock'要求'UITextView'符合'View'

关于如何在文本字段的 VStack 中添加文本视图,您有任何提示吗?

我的代码如下所示:

     VStack {
        Text("Hey")
            .font(.title)
            .foregroundColor(Color.black)
        TextField("Write here", text: $ingredients )
               Button(action: {
                let array = (self.ingredients.lowercased().components(separatedBy: " "))

} }

【问题讨论】:

  • 您的括号可能有问题。尝试选择所有代码,然后按“Ctrl i”重新缩进。
  • 等一下,“UITextView”?来自 UIKit,在 SwiftUI 中不起作用。请改用“TextView”。

标签: swift xcode layout textview textfield


【解决方案1】:

您应该在 SwiftUI 中使用Text 用于静态文本和Text Field 用于可编辑文本,并避免将其与 UIKit 混合。

如果实在无法避免,可以看看这个this guide

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 2019-11-08
    • 2021-03-29
    • 1970-01-01
    • 2011-07-26
    • 2019-07-06
    • 2010-12-17
    相关资源
    最近更新 更多