我正在尝试将UIView绑定到键盘,但工作正常,但是当我在一个UITextField中键入内容时,UIView会滑到底部并消失,甚至在隐藏键盘时也不可见。
这是我的绑定键盘功能:
func bindToKeyboard() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), 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 startingFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let endingFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let deltaY = endingFrame.origin.y - startingFrame.origin.y
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
self.frame.origin.y += deltaY
}, completion: nil)
}
在viewDidLoad()
中的用法:
nextBackView.bindToKeyboard()
答案 0 :(得分:0)
您应该使用UIScrollView
,而不是UIView
。 scrollView将具有一个contentView
,除了UIView
之外什么都没有。
向此UIView
(我想说一个contentView)中添加文本字段。
这就是带有约束的情节提要中的样子。
UIScrollView的顶部和底部空间与顶部和底部布局指南对齐。其余的约束很简单。
在下面的代码中,我们检查键盘是否已打开。如果edgeinsets为零,即底部内容插入为零,则表示键盘即将打开。在这里,我们确保从底部开始的插图现在将更改为例如280.0,即contentview被推向顶部280个点。
关闭键盘后,我们将contentInset.bottom
从内容视图的底部移回0点,
func bindToKeyboard() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
//Set default to zero, when keyboard opens it changes to a non zero value. When keyboard closes it is set back to the correct value.
var scrollContentInset = UIEdgeInsets.zero
@objc func keyboardWillChange(notification: NSNotification) {
let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
let endingFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
if self.scrollContentInset.bottom == 0 {
let adjustmentHeight = (endingFrame.height + 20) * (1)
self.scrollView.contentInset.bottom += adjustmentHeight
self.scrollView.scrollIndicatorInsets.bottom += adjustmentHeight
} else{
let adjustmentHeight = (endingFrame.height + 20) * (-1)
self.scrollView.contentInset.bottom += adjustmentHeight
self.scrollView.scrollIndicatorInsets.bottom += adjustmentHeight
}
self.scrollContentInset = self.scrollView.contentInset
}, completion: nil)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
iOS keyboardWillChange:
被多次调用。因此,此处的键盘逻辑略有变化。在键盘打开之前,Y是UIScreen的高度,在打开键盘时,Y向上移动至其方向(左上角为0,0)。这有助于检测键盘是否在打开。
当键盘关闭时,有时由于软键盘(cmd + K
)或按回车键,在这种情况下,键盘的高度会有所不同。对于({cmd + K
),结束帧的高度为54点,而startFrame的高度为355.0。当以355.0的高度打开键盘时,它应偏移相同的量。所以我在这里使用startFrame和EndFrame的最大高度。
如果在软键盘模式下打开键盘,startFrame
和endFrame
的Y将相同,因此在这种情况下,contentInset不会被修改。
我希望这有助于回答键盘问题。
@objc func keyboardWillChange(notification: NSNotification) {
let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
let startFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let endingFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
if startFrame.minY > endingFrame.minY {
/*
*Keyboard opening
*/
let adjustmentHeight = CGFloat.maximum(startFrame.height, endingFrame.height)
self.scrollView.contentInset.bottom = adjustmentHeight
self.scrollView.scrollIndicatorInsets.bottom = adjustmentHeight
} else if startFrame.minY < endingFrame.minY{
/*
*Keyboard Closing
*/
let adjustmentHeight = CGFloat.maximum(endingFrame.height, startFrame.height) * (-1)
self.scrollView.contentInset.bottom += adjustmentHeight
self.scrollView.scrollIndicatorInsets.bottom += adjustmentHeight
}
}, completion: nil)
}
我在这里发现的另一件事是,在横向模式(landscapeRight
)中,起始帧高度为277.0,而终止帧高度为209.0。而当我们最小化键盘时,它是209。这是在设备处于potrait模式下,然后您在landsappe right模式下打开键盘时发生的。
所以您可能希望键盘打开时的调整高度为endFrameheight
if startFrame.minY > endingFrame.minY {
let adjustmentHeight = endingFrame.height
.........
}