【发布时间】:2017-08-02 13:23:58
【问题描述】:
首先,我没有故事板,一切都是程序化的。我有三个TextField,其中一个隐藏在登录按钮后面(isHidden = true),登录按钮下方是注册按钮。如果您点击注册按钮,登录按钮会滑到注册按钮下方,然后隐藏的 textField 将其 isHidden 属性设置为 false。
目前我的问题是,当点击注册按钮时,登录按钮会向下移动,并且文本字段会显示但无法选择,当我尝试选择它时,登录按钮会弹回原来的位置。
当键盘显示和再次向下时,我的视图也会向上移动,我认为这没有帮助。
文本字段:
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
})
}
查看控制器键盘功能:
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
}
任何建议将不胜感激。谢谢,如果需要更多上下文,请告诉我。所有视图都使用约束固定,不会出现涉及约束的错误。
【问题讨论】:
-
按下注册按钮时添加这行代码。 self.view.bringSubview(toFront: confirmPasswordTextField) 然后尝试选择该字段。
-
非常感谢! textField 可以工作,但添加它现在会使登录按钮跳起来并滑到新显示的文本字段后面。有什么想法吗?
-
@WyneRumble 按下注册按钮时,您必须对所有字段执行相同操作。还必须相应地设置按钮的框架,以免它们相互混淆。
-
我将其发布为答案。请接受。谢谢
-
你解决了登录按钮的问题吗?