无法选择SwiftUI TextField

时间:2019-07-25 12:21:45

标签: ios swift swiftui

我无法选择任何文本并将其输入到我的文本字段中。如果我将它们从当前的VStack中移出并放入第一个VStack中,它们将按预期工作。它们下方的按钮可以在任何位置正常工作,因此文本字段似乎没有任何痕迹。

这是我当前的布局。

import SwiftUI

struct ContentView: View {
    @State var emailAddress = ""
    @State private var password = ""
    @State var showingAlert = false
    @State var error = ""
    @State var showCollections = false

    var body: some View {
        ZStack(alignment: .top) {
            LinearGradient(gradient: Gradient(colors: [.blue, .white,]), startPoint: .bottom, endPoint: .top)
            .edgesIgnoringSafeArea(.all)

            VStack(alignment: .leading) {
                Text("Title")
                    .font(.largeTitle)
                    .frame(width: UIScreen.main.bounds.size.width - 40, height: 100, alignment: .center)
                    .foregroundColor(.blue)

                ZStack(alignment: .top) {
                    Color.white
                    VStack(alignment: .leading) {

                        TextField("Email", text: $emailAddress, onEditingChanged: { edit in

                        }, onCommit: {

                        })
                            .frame(width: UIScreen.main.bounds.size.width - 80, height: 41, alignment: .center)
                            .textFieldStyle(.plain)
                            .background(Color.init(red: 211.0/255.0, green: 211.0/255.0, blue: 211.0/255.0))
                            .textContentType(.emailAddress)
                            .cornerRadius(10)
                            .multilineTextAlignment(.center)
                            .padding(.top, 15)
                            .padding(.bottom, 10)
                            .foregroundColor(.blue)

                        SecureField("Password", text: $password)
                            .frame(width: UIScreen.main.bounds.size.width - 80, height: 41, alignment: .center)
                            .textFieldStyle(.plain)
                            .background(Color.init(red: 211.0/255.0, green: 211.0/255.0, blue: 211.0/255.0))
                            .textContentType(.password)
                            .cornerRadius(10)
                            .multilineTextAlignment(.center)
                            .foregroundColor(.blue)

                        Button(action: signIn) {
                            Text("Sign In")
                                .foregroundColor(.white)
                        }
                        .frame(width: UIScreen.main.bounds.size.width - 80, height: 50, alignment: .center)
                        .background(Color.blue)
                        .cornerRadius(20, antialiased: true)

                        Button(action: createAccount) {
                            Text("Don't have an account? Create one.")
                                .foregroundColor(.blue)
                        }
                        .frame(width: UIScreen.main.bounds.size.width - 80, height: 50, alignment: .center)

                        Button(action: forgotPassword) {
                            Text("Forgot password?")
                                .foregroundColor(.blue)
                        }
                        .frame(width: UIScreen.main.bounds.size.width - 80, height: 50, alignment: .center)
                    }
                }
                .frame(width: UIScreen.main.bounds.size.width - 40, height: 250, alignment: .center)
                .cornerRadius(20)
            }
        }
    }

    func signIn() {

    }

    func createAccount() {

    }

    func forgotPassword() {

    }
}

#if DEBUG
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

enter image description here

1 个答案:

答案 0 :(得分:2)

确实发生了一些奇怪的事情,但是,您可以通过在zIndex()TextField上使用SecureField来解决此问题:

TextField("Email", text: $emailAddress, onEditingChanged: { edit in }, onCommit: { })
    .frame(width: UIScreen.main.bounds.size.width - 80, height: 41, alignment: .center)
    .textFieldStyle(.plain)
    .background(Color.init(red: 211.0/255.0, green: 211.0/255.0, blue: 211.0/255.0))
    .textContentType(.emailAddress)
    .cornerRadius(10)
    .multilineTextAlignment(.center)
    .padding(.top, 15)
    .padding(.bottom, 10)
    .foregroundColor(.blue)
    .zIndex(1)

SecureField("Password", text: $password)
    .frame(width: UIScreen.main.bounds.size.width - 80, height: 41, alignment: .center)
    .textFieldStyle(.plain)
    .background(Color.init(red: 211.0/255.0, green: 211.0/255.0, blue: 211.0/255.0))
    .textContentType(.password)
    .cornerRadius(10)
    .multilineTextAlignment(.center)
    .foregroundColor(.blue)
    .zIndex(1)