gpt4 book ai didi

android - 在 iOS 中管理键盘打开和隐藏时的屏幕显示和底部按钮与 Android 相同

转载 作者:行者123 更新时间:2023-11-30 10:57:52 24 4
gpt4 key购买 nike

预期功能与 Android 设备中的功能相同(查看下图):

When Keyboard is not open Android

当键盘打开时,底部按钮会向上动画化,并且屏幕也会向上移动一点,具体取决于设备屏幕尺寸。检查下面的图片

When Keyboard is Open Android

我正在尝试在我的 iOS 应用程序中实现相同的功能。这是我的代码:

    @objc func keyboardWillChangeFrame(_ notification: Notification?) {
guard let keyboardRect = (notification?.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {return}

let screenY = UIScreen.main.bounds.height
let shrunkViewHeight = screenY - keyboardRect.height
self.forwardButton.frame.origin.y = keyboardRect.origin.y
if (shrunkViewHeight < screenY/1.5) {
let screenShift = ((screenY/2) - shrunkViewHeight)
if notification?.name == UIResponder.keyboardWillShowNotification || notification?.name == UIResponder.keyboardWillChangeFrameNotification {
UIView.animate(withDuration: 1.0) {
self.view.frame.origin.y = screenShift

}
}else{
UIView.animate(withDuration: 1.0) {
self.view.frame.origin.y = 0
}
}
}
}

目前发生的事情:该按钮有时会保留在底部,特别是当第一响应者在不关闭键盘的情况下从一个 TextField 更改为另一个 TextField 时。

预期解决方案:上述功能应适用于从 iPhone 6 到最新 XR 的所有 iPhone。接受第三方建议。

最佳答案

swift 3

@IBOutlet weak var BottomConstraint: NSLayoutConstraint! //your last item in bottom of your view controller constraint

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)

self.registerForKeyboardNotifications()
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

self.deregisterFromKeyboardNotifications()
}


func registerForKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}

func deregisterFromKeyboardNotifications() {
NotificationCenter.default.removeObserver(self)
}

func keyboardNotification(_ notification: Foundation.Notification) {

if let userInfo = notification.userInfo {

let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let duration:TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIViewAnimationOptions().rawValue
let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)

if (endFrame?.origin.y)! >= UIScreen.main.bounds.size.height {
self.sendCmntViewBottomConstraint?.constant = 0.0
} else {
self.sendCmntViewBottomConstraint?.constant = endFrame!.size.height
}

UIView.animate(withDuration: duration, delay: TimeInterval(0), options: animationCurve, animations: { self.view.layoutIfNeeded() }, completion: nil)

}
}

关于android - 在 iOS 中管理键盘打开和隐藏时的屏幕显示和底部按钮与 Android 相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53736670/

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