gpt4 book ai didi

ios - 优雅地将 UIView 移动到键盘的 inputAccessoryView

转载 作者:行者123 更新时间:2023-12-01 18:09:52 24 4
gpt4 key购买 nike

有没有一种优雅的方式来无缝移动 UIView (即 UIButton ),位于其父 View 的底部,到键盘的 inputAccessoryView ?

我设想键盘在滑入时基本上会拿起按钮并将其向上拖动。请参阅下面的模型。显然,我希望这也能反过来工作。

我知道如何分别做,但我不知道如何结合起来。有谁以前这样做过吗?

谢谢!

Illustration of the idea

更新:我忘了提到我主要在 UIScrollViews 的上下文中工作。 .虽然下面安德烈的答案适用于普通 View ,但在将其与 ScrollView 一起使用时会中断。

最佳答案

在这种情况下,您可以忽略使用 inputaccessoryview,而是调整登录按钮对键盘通知的底部约束。

class ViewController: UIViewController {
@IBOutlet weak var signInButtonBottomConstraint: NSLayoutConstraint!

override func viewDidLoad() {
super.viewDidLoad()

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}

func keyboardWillShow(notification: NSNotification) {
let userInfo = notification.userInfo!

let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double
let keyboardEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()

signInButtonBottomConstraint.constant = keyboardEndFrame.height
UIView.animateWithDuration(animationDuration) { () -> Void in
self.view.layoutIfNeeded()
}
}

func keyboardWillHide(notification: NSNotification) {
let userInfo = notification.userInfo!

let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double

signInButtonBottomConstraint.constant = 0.0
UIView.animateWithDuration(animationDuration) { () -> Void in
self.view.layoutIfNeeded()
}
}
}

关于ios - 优雅地将 UIView 移动到键盘的 inputAccessoryView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33679295/

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