很抱歉,如果有任何类似的问题得到解答。但是,我似乎无法想象这一个。
我已达到目标,将“登录”按钮绑定到键盘,基本上使用下面的扩展将其从屏幕底部推到键盘顶部。
picture: Initial view without keyboard.
picture: keyboard has been launched.
我的UIView
扩展:
import UIKit
extension UIView{
func bindToKeyboard(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
@objc func keyboardWillChange(_ notification: NSNotification){
let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
let beginningFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let endFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let deltaY = endFrame.origin.y - beginningFrame.origin.y
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
self.frame.origin.y += deltaY
}, completion: nil)
}
}
我在我的LoginVC的bindToKeyboard()
中将loginBtn
称为viewDidLoad()
,如下所示:
loginBtn.bindToKeyboard()
这里的问题是,在第一次点击文本字段(电子邮件或密码字段)后,按钮消失。键盘关闭后,按钮实际上回到了初始位置,就像在第一张图片中一样。然后通过点击其中一个文本字段再次调用键盘,按钮正常工作。但是第二次等等,它没有。
我的问题的要点:
如果我的解释和/或英语不清楚,我很抱歉。
非常感谢。
在此动画中,如果使用框架来控制按钮的位置,则按钮应该在垂直方向上没有约束。
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
self.frame.origin.y += deltaY
}, completion: nil)
在从UIButton中删除所有约束后,我很好地使用了这个动画。否则,self.frame.origin.y += deltaY
应该用约束常量替换。
移动按钮很幸运。
我有完全相同的问题,最初我尝试通过UIKeyboardNotification
方法处理它,但问题是当键盘更改其状态时编辑不同的UITextField
时,它将不会注册任何状态更改,因为它已经处于活动状态。因此,经过多次探索后,我通过创建一个带有新按钮的accessoryView来处理它,该按钮是我实际按钮的重复。所以基本上,考虑你有一个UIButton
坚持在UIViewController
的底部,你有2 UITextField
,当交换切换时,不能感觉UIButton
曾经移动到别处,但仍然卡在键盘上。以下代码解释了如何解决此问题:
@IBOutlet weak var signInBtn: UIButton!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
var accessoryViewKeyboard:UIView?
var btnAccessory:UIButton?
override func viewDidLoad() {
super.viewDidLoad()
passwordTextField.delegate = self
emailTextField.delegate = self
accessoryViewKeyboard = UIView(frame: signInBtn.frame)
//Inputting the "accessoryViewKeyboard" here as the "inputAccessoryView" is of
//utmost importance to help the "signInBtn" to show up on tap of different "UITextFields"
emailTextField.inputAccessoryView = accessoryViewKeyboard
passwordTextField.inputAccessoryView = accessoryViewKeyboard
setupBtnWithKeyboard()
}
func setupBtnWithKeyboard() {
btnAccessory = UIButton(frame: CGRect(x: signInBtn.frame.origin.x, y: signInBtn.frame.origin.y, width: self.view.frame.size.width, height: signInBtn.frame.size.height))
accessoryViewKeyboard?.addSubview(btnAccessory!)
btnAccessory?.translatesAutoresizingMaskIntoConstraints = false
btnAccessory?.frame = CGRect(x: (accessoryViewKeyboard?.frame.origin.x)!,
y: (accessoryViewKeyboard?.frame.origin.y)!,
width: self.view.frame.size.width,
height: (accessoryViewKeyboard?.frame.size.height)!)
btnAccessory?.backgroundColor = UIColor(red: 31/255, green: 33/255, blue: 108/255, alpha: 1)
btnAccessory?.setTitle("Sign In", for: .normal)
btnAccessory?.titleLabel?.font = .systemFont(ofSize: 22)
btnAccessory?.titleLabel?.textColor = UIColor.white
btnAccessory?.titleLabel?.textAlignment = .center
btnAccessory?.isEnabled = true
btnAccessory?.addTarget(self, action: #selector(SignIn.signInBtnPressed), for: .touchUpInside)
NSLayoutConstraint.activate([
btnAccessory!.leadingAnchor.constraint(equalTo:
accessoryViewKeyboard!.leadingAnchor, constant: 0),
btnAccessory!.centerYAnchor.constraint(equalTo:
accessoryViewKeyboard!.centerYAnchor),
btnAccessory!.trailingAnchor.constraint(equalTo:
accessoryViewKeyboard!.trailingAnchor, constant: 0),
btnAccessory!.heightAnchor.constraint(equalToConstant: signInBtn.frame.size.height),
])
}
而且你已经完成了。这将使UIButton
始终出现在键盘上。重要的是无论你介绍多少UITextField
实例,总是输入accessoryViewKeyboard
作为其inputAccessoryView
。