gpt4 book ai didi

ios - isHidden 设置为 false 后无法选择隐藏的 textField

转载 作者:行者123 更新时间:2023-11-28 15:24:33 25 4
gpt4 key购买 nike

首先,我没有 Storyboard,一切都是程序化的。我有三个 TextFields,其中一个隐藏(isHidden = true)在登录按钮后面,登录按钮下方是注册按钮。如果您点击注册按钮,登录按钮会向下滑动到注册按钮下方,然后隐藏的文本字段会将其 isHidden 属性设置为 false。

我目前的问题是,当点击注册按钮时,登录按钮会向下移动并显示文本字段,但无法被选中,当我尝试选择它时,登录按钮会弹回到原来的位置。

我还让 View 在显示键盘时向上移动,然后再次向下移动,我认为这没有帮助。

文本字段:

class SplitterTextField: UITextField, UITextFieldDelegate {

var accessID: String!

required init(frame: CGRect, accessID: String) {
super.init(frame: frame)
self.accessID = accessID
setup()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

private func setup() {
delegate = self
backgroundColor = Color.textFieldBackground
accessibilityIdentifier = accessID
textAlignment = .center
returnKeyType = .done
placeholder = NSLocalizedString("\(accessID!)PlaceHolder", comment: "")
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
resignFirstResponder()
return true
}
}

移动按钮功能:

@objc private func registerButtonTapped() {
if confirmPasswordTextField.isHidden {
animateLoginButton()
} else {
registerNewUser()
}
}


@objc private func loginButtonTapped() {
if !confirmPasswordTextField.isHidden {
animateLoginButton()
} else {
//segue to next vc
}
}

private func animateLoginButton() {
if confirmPasswordTextField.isHidden {
moveLoginButtonDown()
} else {
moveLoginButtonUp()
}
}

private func moveLoginButtonDown() {
//Move loginButton down revealing confirmationPasswordTextView behind it
UIView.animate(withDuration: 0.3, animations: {
self.loginButton.frame.origin.y += Layout.loginButtonYMovement
self.confirmPasswordTextField.isHidden = false
})
}

private func moveLoginButtonUp() {
//Move the loginButton up, when it has finished moving hide the confirmationPasswordTextView
UIView.animate(withDuration: 0.3, animations: {
self.loginButton.frame.origin.y -= Layout.loginButtonYMovement
}, completion: { _ in
self.confirmPasswordTextField.isHidden = true
})
}

查看 Controller 键盘功能:

func setupKeyboard() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(sender:)), name: NSNotification.Name.UIKeyboardWillShow,object: nil)
NotificationCenter.default.addObserver(self,selector: #selector(keyboardWillHide(sender:)),name: NSNotification.Name.UIKeyboardWillHide,object: nil)

}

@objc private func keyboardWillShow(sender: NSNotification) {
self.view.frame.origin.y = Layout.welcomeScreenKeyboardMovement
}

@objc private func keyboardWillHide(sender: NSNotification) {
self.view.frame.origin.y = 0
}

如有任何建议,我们将不胜感激。谢谢,让我知道是否需要更多上下文。所有 View 都使用约束固定,并且不会发生涉及约束的错误。

最佳答案

当按下注册按钮时添加这行代码。

self.view.bringSubview(toFront: confirmPasswordTextField) 

然后尝试选择该字段。正确设置框架按钮,使它们不会相互混淆。您可以打印按钮框架以便更好地理解。

关于ios - isHidden 设置为 false 后无法选择隐藏的 textField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45462144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com