微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SwiftUI 由于键盘而自动向上推

如何解决SwiftUI 由于键盘而自动向上推

对我来说,即使在 stackOverflow 上提供了答案,我的视图仍然会被键盘推高。我两个都用:

  • .ignoresSafeArea(.keyboard,edges: .bottom) - 也在主视图中
  • 以及代码开头和结尾的两个 Spacer()

对不起,如果这里缺少细节,这是我第一篇关于堆栈溢出的真实帖子

import SwiftUI

struct LoginView:查看{

// needed to get keyboard height when keyboard is shown
@StateObject private var keyboardHandler = KeyboardHandler()

//Sobald login gedrückt wurde und Erfolgreich war wird die Variable True
@State var isActive: Bool = false
@Observedobject private var loginVM = Loginviewmodel()

// change the var for the main animation
@Binding var userIsLoggedIn: Bool

// if a problem happens during the login process a message can be shown
@State var problemAccured = false

// message to present the user when error happend
@State var problemmessage = "Wrong password"

var body: some View {
    vstack {
        
        Spacer()
        
        // stack with textfields
        vstack(alignment: .leading) {
            
            if problemAccured {
                Text(problemmessage)
                    .font(.footnote)
                    .padding(.leading,35)
            }
            
            HStack(spacing: 12) {
                Image(systemName: "envelope")
                TextField("Enter your email address here",text: $loginVM.email)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .keyboardType(.emailAddress)
            }
            .padding(.bottom,3)
            
            Divider()
            
            // main password textField
            HStack(spacing: 12) {
                Image(systemName: "lock.shield")
                SecureField("Enter your password here",text: $loginVM.password)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                
            }
            .padding(.top,3)
            
            
        }
        .font(.subheadline)
        .foregroundColor(.accentColor)
        .padding()
        
        .modifier(FrozenWindowModifier())
        .frame(width: screen.width - 40)
        .offset(y: keyboardHandler.keyboardHeight/10)
        .animation(.easeInOut(duration: 0.3))
        
        
        Spacer()
        
        // login button
        Button(action: {
            // haptic Feedback when button is tapped
            hapticpulse(Feedback: .rigid)
            
            // login handleling
            loginVM.login {
                isActive = true
                print("Login succeeded \(isActive)")
                userIsLoggedIn = true
            }
            
            // Show textfield if errors accure
            problemAccured = loginVM.checkerrors()
            // Set Problem Message to ErrorMessage
            problemmessage = loginVM.errorMessage
            
        },label: {
            HStack {
                Text("Login!")
                    .foregroundColor(.primary)
                Image(systemName: "person.fill.checkmark")
            }
        })
        .padding()
        
        // get the standard border and clipShape of the window
        .modifier(WindowModifier())
        // animation when keyboard is shown or not
        .padding(.bottom,keyboardHandler.keyboardHeight)
        .animation(.easeInOut(duration: 0.3))
    }
}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。