将按钮 (UIView) 绑定到在多个文本字段中工作的键盘 (Swift 4.2) [英] Binding a button (UIView) to keyboard that works in multiple textfields (Swift 4.2)

查看:27
本文介绍了将按钮 (UIView) 绑定到在多个文本字段中工作的键盘 (Swift 4.2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,如果有人回答过类似的问题.但是,我似乎无法弄清楚这一点.

Sorry if any similar question(s) has been answered. But, I just can't seem to figure this one out.

我已经达到了我的目标,将登录"按钮绑定到键盘,基本上是使用下面的扩展将它从屏幕底部推到键盘顶部.

I have reached my goal, to bind the "Log In" button to the keyboard, basically pushing it from the bottom of the screen to the top of the keyboard using an extension below.

图片:无键盘的初始视图.

图片:键盘已经上线.

我的 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 的 viewDidLoad() 中调用 bindToKeyboard()loginBtn,如下所示:

and I called bindToKeyboard() to loginBtn in the viewDidLoad() of my LoginVC like so:

loginBtn.bindToKeyboard()

这里的问题是,第一次点击文本字段(电子邮件或密码字段)后,按钮消失.键盘关闭后,按钮实际上回到了它的初始位置,就像第一张图一样.然后通过点击这些文本字段之一再次调用键盘,按钮正常工作.但是第二次等点击,它没有.

The problem here is, after the first tap to the textfield (either email or password field), the button disappears. After the keyboard is closed, the button is actually back to its initial position just like in the first picture. Then calling the keyboard again by tapping one of those textfields, the button works properly. But the second and so forth tap, it does not.

我的问题的重点:

  1. 如何实现扩展才能与多个文本字段/文本视图一起正常工作?
  2. 如果这不可能,我应该如何解决这个问题?

如果我的解释和/或英语不清楚,我很抱歉.

I am sorry if my explanation and or English is unclear.

非常感谢.

推荐答案

在这个动画中,如果使用frame来控制按钮的位置,按钮应该是在垂直方向上不受约束的.

In this animation, if you use frame to control the position of button, the button is supposed to be free of constrains in vertical direction.

    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 应替换为约束常量.

I use this animation well after removing all the constraints from UIButton. Otherwise, self.frame.origin.y += deltaY should be replaced with constraint constant.

幸运的是移动按钮.

这篇关于将按钮 (UIView) 绑定到在多个文本字段中工作的键盘 (Swift 4.2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆