首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITextFields没有在结构中显示,如何修复?

UITextFields没有在结构中显示,如何修复?
EN

Stack Overflow用户
提问于 2020-08-19 02:34:45
回答 1查看 35关注 0票数 1

我正在尝试在我的应用程序上有某种类型的提交表单,但由于某些原因,我无法放置UItextfields。我在这个结构的开头声明了它们,但是当涉及到显示时,我得到了错误:

“类型'()‘不能符合'View';只有struct/enum/class类型才能符合协议”

代码语言:javascript
复制
import SwiftUI
import UIKit

/// Shows a list of inspiring quotes/bio
struct FeatureView: View {
    
    @State private var userEmail: UITextField = UITextField(frame: CGRect(x:10, y:320, width:300.00, height:30.00))
    
    private var instagramName: UITextField = UITextField(frame: CGRect(x:10, y:320, width:300.00, height:30.00))
    
    var body: some View {
        VStack {
            HStack {
                VStack(alignment: .leading) {
                    Text("Get Featured").font(.largeTitle).bold()
                    Spacer()
                    Text("1. Submit your information below")
                    Spacer()
                    Text("2. On the 1st and 14th we choose a new account to feature")
                    Spacer()
                    Text("3. Get notified by e-mail if your account is chosen!")
                    Spacer()
                }
                Spacer()
                userEmail.placeholder = "email"
                userEmail.addSubview(userEmail)
            }.padding(EdgeInsets(top: 20, leading: 20, bottom: 0, trailing: 20))
            
        }
    }
}

我只是想让一个电子邮件字段显示和可编辑的现在,但最终我想要5-6个不同的字段,所有采取的信息,我最终可以让用户提交

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-19 03:09:22

您必须使用TextField。如果你想使用UIKit,你必须将UITextField封装到UIViewRepresentable中。

下面是如何使用TextField,您需要创建两个@State属性来跟踪每个TextField的文本

代码语言:javascript
复制
import SwiftUI

/// Shows a list of inspiring quotes/bio
struct FeatureView: View {

    @State private var emailText: String = ""
    @State private var instagramNameText: String = ""

    var body: some View {
        VStack {
            HStack {
                VStack(alignment: .leading) {
                    Text("Get Featured").font(.largeTitle).bold()
                    Spacer()
                    Text("1. Submit your information below")
                    Spacer()
                    Text("2. On the 1st and 14th we choose a new account to feature")
                    Spacer()
                    Text("3. Get notified by e-mail if your account is chosen!")
                    Spacer()
                }
                Spacer()
            }.padding(EdgeInsets(top: 20, leading: 20, bottom: 0, trailing: 20))

            VStack {
                TextField("Email", text: $emailText)
                    .frame(width: 300, height: 30)
                    .padding(.leading, 10)
                TextField("Instagram name", text: $instagramNameText)
                    .frame(width: 300, height: 30)
                    .padding(.leading, 10)
            }
        }
    }
}

下面是如何用UIViewRepresentable包装它以使用UITextField

代码语言:javascript
复制
import SwiftUI

/// Shows a list of inspiring quotes/bio
struct FeatureView: View {

    private let emailTextField = CustomWrappedTextField(customPlaceholder: "Email")
    private let instagramTextField = CustomWrappedTextField(customPlaceholder: "Instagram name")

    var body: some View {
        VStack {
            HStack {
                VStack(alignment: .leading) {
                    Text("Get Featured").font(.largeTitle).bold()
                    Spacer()
                    Text("1. Submit your information below")
                    Spacer()
                    Text("2. On the 1st and 14th we choose a new account to feature")
                    Spacer()
                    Text("3. Get notified by e-mail if your account is chosen!")
                    Spacer()
                }
                Spacer()
            }.padding(EdgeInsets(top: 20, leading: 20, bottom: 0, trailing: 20))

            VStack {
                emailTextField
                instagramTextField
            }
        }
    }
}

final class CustomWrappedTextField: UIViewRepresentable {

    typealias UIViewType = UITextField

    let customPlaceholder: String

    init(customPlaceholder: String) {
        self.customPlaceholder = customPlaceholder
    }

    func makeUIView(context: Context) -> UITextField {

        let customTextField = UITextField(frame: CGRect(x: 10, y: 320, width: 300.00, height: 30.00))

        customTextField.placeholder = customPlaceholder

        return customTextField
    }

    func updateUIView(_ uiView: UITextField, context: Context) {

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63474686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档