有一个带有两个UITextFields的视图,该视图根据键盘的高度移动。但是,如果至少有一个textfield的isSecureTextEntry属性设置为true,则移动视图并不理想。当在文本字段之间移动时,这一点很明显。视图下移一次,然后立即返回。这是因为键盘的高度会随时发生变化。我认为这是一个显示/隐藏QuickTypeBar的问题。那么,如何消除这种影响并控制视图移动呢?
下面是所有的代码。它在模拟器中可能看起来不正确。因为没有显示QuickType栏。这发生在iPhone X iOS13.3.1上。
谢谢。


import UIKit
class ViewController: UIViewController {
public var boardView: UIView!
public var usernameTextField: UITextField!
public var passwordTextField: UITextField!
public var boardViewBottomAnchorConstraint: NSLayoutConstraint!
public var boardViewBottomAnchorConstraintConstant: CGFloat = -220
public var offset: CGFloat = 16.0
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .darkGray
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeFrame(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
var constraints = [NSLayoutConstraint]()
let boardView = UIView()
boardView.backgroundColor = .brown
boardView.layer.cornerRadius = 16.0
boardView.translatesAutoresizingMaskIntoConstraints = false
boardViewBottomAnchorConstraint = boardView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: boardViewBottomAnchorConstraintConstant)
constraints.append(boardView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 16))
constraints.append(boardViewBottomAnchorConstraint)
constraints.append(boardView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -16))
view.addSubview(boardView)
self.boardView = boardView
let usernameTextField = UITextField()
usernameTextField.backgroundColor = .white
usernameTextField.delegate = self
usernameTextField.returnKeyType = .next
usernameTextField.textContentType = .username
usernameTextField.placeholder = "USERNAME TEXTFIELD"
usernameTextField.translatesAutoresizingMaskIntoConstraints = false
constraints.append(usernameTextField.heightAnchor.constraint(equalToConstant: 56))
constraints.append(usernameTextField.topAnchor.constraint(equalTo: boardView.topAnchor, constant: 100))
constraints.append(usernameTextField.leftAnchor.constraint(equalTo: boardView.leftAnchor, constant: 24))
constraints.append(usernameTextField.rightAnchor.constraint(equalTo: boardView.rightAnchor, constant: -24))
boardView.addSubview(usernameTextField)
self.usernameTextField = usernameTextField
let passwordTextField = UITextField()
passwordTextField.backgroundColor = .white
passwordTextField.delegate = self
passwordTextField.returnKeyType = .go
passwordTextField.textContentType = .password
passwordTextField.placeholder = "PASSWORD TEXTFIELD"
passwordTextField.translatesAutoresizingMaskIntoConstraints = false
constraints.append(passwordTextField.heightAnchor.constraint(equalToConstant: 56))
constraints.append(passwordTextField.topAnchor.constraint(equalTo: usernameTextField.bottomAnchor, constant: 50))
constraints.append(passwordTextField.leftAnchor.constraint(equalTo: boardView.leftAnchor, constant: 24))
constraints.append(passwordTextField.bottomAnchor.constraint(equalTo: boardView.bottomAnchor, constant: -100))
constraints.append(passwordTextField.rightAnchor.constraint(equalTo: boardView.rightAnchor, constant: -24))
boardView.addSubview(passwordTextField)
self.passwordTextField = passwordTextField
NSLayoutConstraint.activate(constraints)
let tap = UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing(_:)))
view.addGestureRecognizer(tap)
// false is the ideal move. If true, the view moves violently.
passwordTextField.isSecureTextEntry = true
}
}
extension ViewController {
@objc
public func keyboardWillShow(_ notification: Notification) { print("WILL SHOW")
updateConstraints(notification)
}
@objc
public func keyboardWillHide(_ notification: Notification) { print("WILL HIDE")
boardViewBottomAnchorConstraint.constant = boardViewBottomAnchorConstraintConstant
view.layoutIfNeeded()
}
@objc
public func keyboardWillChangeFrame(_ notification: Notification) { print("WILL CHANGE FRAME")
updateConstraints(notification)
}
private func updateConstraints(_ notification: Notification) {
guard
let toFrame = notification.userInfo?[UIApplication.keyboardFrameEndUserInfoKey] as? CGRect,
let fromFrame = notification.userInfo?[UIApplication.keyboardFrameBeginUserInfoKey] as? CGRect else { return }
print(fromFrame); print(toFrame);
let constant = -toFrame.size.height - offset
boardViewBottomAnchorConstraint.constant = constant
view.layoutIfNeeded()
}
}
extension ViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == usernameTextField {
passwordTextField.becomeFirstResponder()
} else {
passwordTextField.resignFirstResponder()
}
return true
}
}发布于 2020-04-10 00:22:19
if UIApplication.shared.isKeyboardPresented {
print("Keyboard presented")
} else {
print("Keyboard is not presented")
}将此代码与您的代码结合使用。确定键盘何时处于活动状态并执行相应的操作
编辑
YOu需要在按住Ctrl键的同时从每个文本字段拖动到代码中,并创建一个@IBAction
@IBAction func textField1() {
//when a user taps on text field 1. this code block is triggered.
if UIApplication.shared.isKeyboardPresented {
print("Keyboard presented")
//if the keyboard is already presented do whatever other action is required
//inside here the keyboard is already active
} else {
print("Keyboard is not presented")
//if the keyboard is not currently active do any required action here
//Inside here the keyboards will become active
}
}对于所有其他文本字段和所有其他视图,您需要执行相同的操作并根据操作自定义代码
EDI
下面是如何在不使用接口构建器的情况下实现相同的功能
let textField = UITextField()
self.textField.addTarget(self, action: #selector(targetFunc), for: .touchUpInside)
@objc func targetFunc() {
// same actions as above in here
}发布于 2020-04-10 00:59:26
尝试使用cocoapods中的github.com/hackiftekhar/IQKeyboardManager。在文档中,您必须在appdelegate类中添加几行代码,这样就可以了。IQKeyboardmanager可以通过cocoapods和IOS快速依赖管理器获得。
https://stackoverflow.com/questions/61124841
复制相似问题