gpt4 book ai didi

ios - 当键盘可见时更改 Y 原点时出现奇怪的 UIView 行为

转载 作者:搜寻专家 更新时间:2023-10-30 23:01:22 25 4
gpt4 key购买 nike

上下文:

我有一个包含 UITextView 的 UIView。我想在键盘可见时向上移动 UIView。所以想法是change the Y position .

问题:

有时,当键盘仍然可见时,UIView 会返回到其初始 Y 位置。即使我专注于输入,它也不会再回来了。我真的不知道为什么。

插图 (gif):

https://d17oy1vhnax1f7.cloudfront.net/items/2K0z3P1j1x2f0X2X1B2v/ci.gif

PS:键盘在Gif中出现了3次。它是循环的,所以很难区分结束。

代码

我做了什么,首先我添加了通知观察者:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillChangeFrame), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)

然后我定义了键盘切换处理程序:

func keyboardWillChangeFrame(notification: NSNotification) {

let info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue

self.keyboardInfo["isVisible"] = self.view.frame.size.height - keyboardFrame.origin.y != 0
self.keyboardInfo["height"] = keyboardFrame.size.height
self.keyboardInfo["animationDuration"] = info[UIKeyboardAnimationDurationUserInfoKey] as! Double
self.keyboardInfo["animationCurve"] = info[UIKeyboardAnimationCurveUserInfoKey] as! UInt

if self.keyboardInfo["isVisible"] as! Bool {
self.moveCommentInputUp()
} else {
self.moveCommentInputDown()
}

}

之后,我定义了将评论输入 View 向上/向下移动的方法:

func moveCommentInputUp() {

print("Moving up... ", self.commentFormView.frame.origin.y, " to ", self.view.frame.size.height - (self.keyboardInfo["height"] as! CGFloat) - self.commentFormView.frame.size.height)

UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(self.keyboardInfo["animationDuration"] as! TimeInterval)
UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: Int(self.keyboardInfo["animationCurve"] as! UInt))!)
UIView.setAnimationBeginsFromCurrentState(true)

ViewUtil.changeViewFrame(view: self.commentFormView, yPosition: self.view.frame.size.height - (self.keyboardInfo["height"] as! CGFloat) - self.commentFormView.frame.size.height)
ViewUtil.removeShadowToView(self.commentFormLauncherButton)

UIView.commitAnimations()

}

func moveCommentInputDown() {

print("Moving down... ", self.commentFormView.frame.origin.y, " to ", self.view.frame.size.height)

UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(self.keyboardInfo["animationDuration"] as! TimeInterval)
UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: Int(self.keyboardInfo["animationCurve"] as! UInt))!)
UIView.setAnimationBeginsFromCurrentState(true)

ViewUtil.changeViewFrame(view: self.commentFormView, yPosition: self.view.frame.size.height)
ViewUtil.addShadowToView(commentFormLauncherButton, position: CGSize(width: 0, height: 5))

UIView.commitAnimations()

}

按钮的 Action 是

@IBAction func showCommentForm(_ sender: UIButton) {

self.commentInput.becomeFirstResponder()

}

日志(请参阅 Gif)显示 View 从 568(屏幕底部)再次移动到 568

// The numbers show the initial and the final value of the keyboard Y position

// Click on button to focus the input
Moving up... 568.0 to 282.0
// Tap anywhere
Moving down... 282.0 to 568.0

// Click again on button to focus the input
Moving up... 568.0 to 282.0
///// Write "hhh". The view is gone, I don't know why.
// Tap anywhere
Moving down... 568.0 to 568.0 // The view seems to move from 568 to 568, hugh

// Click on button to focus the input
Moving up... 568.0 to 282.0 // This didn't work apparently
// Tap anywhere
Moving down... 568.0 to 568.0 // 568 to 568 again

我错过了什么吗?

系统:iOS 9、Xcode 8、Swift 3

最佳答案

好的,这是一个不需要 CocoaPods 的解决方案。

我的方法涉及在键盘出现和消失时设置 textField 底部约束的常量。这意味着您需要创建对文本字段底部约束的引用。

@IBOutlet weak var textFieldBottomConstraint: NSLayoutConstraint!

我开始在 viewDidLoad 中设置 NSNotification.Name.UIKeyboardWillShow 和 UIKeyboardWillHide 的观察者,而不是 UIKeyboardWillChangeFrame:

NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

接下来我设置选择器及其函数来设置文本字段底部约束的常量:

func handleKeyboardNotification(notification: NSNotification) {
if let userInfo = notification.userInfo {
let keyboardFrameValue = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)
let keyboardFrame = keyboardFrameValue?.cgRectValue

let isKeyboardShowing = notification.name == NSNotification.Name.UIKeyboardWillShow

textFieldBottomConstraint.constant = isKeyboardShowing ? keyboardFrame!.height + 4 : 4
self.view.layoutIfNeeded()
}
}

请注意,textFieldBottomConstraint 是底部约束的引用变量。

这样做的目的是让 textField 在键盘出现或消失时设置它的底部约束,并且不会遇到您遇到的奇怪故障。

希望这对您有所帮助,请不要犹豫,要求澄清!

致谢 YouTuber 让我们构建该应用程序吧!通过此视频提供解决方案:

https://www.youtube.com/watch?v=p8IaS5lmhuM

关于ios - 当键盘可见时更改 Y 原点时出现奇怪的 UIView 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39604177/

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