我正在尝试在我的应用程序上有某种类型的提交表单,但由于某些原因,我无法放置UItextfields。我在这个结构的开头声明了它们,但是当涉及到显示时,我得到了错误:
“类型'()‘不能符合'View';只有struct/enum/class类型才能符合协议”
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个不同的字段,所有采取的信息,我最终可以让用户提交
发布于 2020-08-19 03:09:22
您必须使用TextField。如果你想使用UIKit,你必须将UITextField封装到UIViewRepresentable中。
下面是如何使用TextField,您需要创建两个@State属性来跟踪每个TextField的文本
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
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) {
}
}https://stackoverflow.com/questions/63474686
复制相似问题