gpt4 book ai didi

ios - 当键盘出现时移动 View 并在点击周围时再次隐藏

转载 作者:行者123 更新时间:2023-11-29 05:45:55 25 4
gpt4 key购买 nike

我有以下 NSLayoutConstraint 类的代码。该类帮助我在键盘出现时移动 View 。很好,而且很有效。

但不知何故,当我将此类分配给约束并且键盘可见时,只要按钮可见,就无法单击按钮。

我遇到这种情况是因为我有另一种和平的代码,当我点击它时它会隐藏键盘 - 一旦我删除它,我就可以在键盘可见时点击按钮,但这带来了另一个问题,即键盘点击周围时不会消失。

有没有办法将两者结合起来,以便 View 移动,但如果我点击周围,键盘会再次消失。

点击周围时:

extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}

@objc func dismissKeyboard() {
view.endEditing(true)
}
}

移动 View :

import UIKit

#if !os(tvOS)
@available(tvOS, unavailable)
public class KeyboardLayoutConstraint: NSLayoutConstraint {

private var offset : CGFloat = 0
private var keyboardVisibleHeight : CGFloat = 0

@available(tvOS, unavailable)
override public func awakeFromNib() {
super.awakeFromNib()

offset = constant

NotificationCenter.default.addObserver(self, selector: #selector(KeyboardLayoutConstraint.keyboardWillShowNotification(_:)), name: UIWindow.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(KeyboardLayoutConstraint.keyboardWillHideNotification(_:)), name: UIWindow.keyboardWillHideNotification, object: nil)
}

deinit {
NotificationCenter.default.removeObserver(self)
}

// MARK: Notification

@objc func keyboardWillShowNotification(_ notification: Notification) {
if let userInfo = notification.userInfo {
if let frameValue = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let frame = frameValue.cgRectValue
keyboardVisibleHeight = frame.size.height / 2
}

self.updateConstant()
switch (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber, userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber) {
case let (.some(duration), .some(curve)):

let options = UIView.AnimationOptions(rawValue: curve.uintValue)

UIView.animate(
withDuration: TimeInterval(duration.doubleValue),
delay: 0,
options: options,
animations: {
UIApplication.shared.keyWindow?.layoutIfNeeded()
return
}, completion: { finished in
})
default:

break
}

}

}

@objc func keyboardWillHideNotification(_ notification: NSNotification) {
keyboardVisibleHeight = 0
self.updateConstant()

if let userInfo = notification.userInfo {

switch (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber, userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber) {
case let (.some(duration), .some(curve)):

let options = UIView.AnimationOptions(rawValue: curve.uintValue)

UIView.animate(
withDuration: TimeInterval(duration.doubleValue),
delay: 0,
options: options,
animations: {
UIApplication.shared.keyWindow?.layoutIfNeeded()
return
}, completion: { finished in
})
default:
break
}
}
}

func updateConstant() {
self.constant = (offset - keyboardVisibleHeight)
}

}
#endif

最佳答案

如果您可以使用第三方库,请查看:

https://github.com/hackiftekhar/IQKeyboardManager

这个库非常有用且高度可定制,可以立即完成工作。

关于ios - 当键盘出现时移动 View 并在点击周围时再次隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56151162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com