HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 键盘可见时更改Y原点时的奇怪UIView行为大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
语境:

我有一个包含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()
    }

}

之后,我已经定义了在注释输入视图中向上/向下移动的方法

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.view.frame.size.height)

    UIView.beginAnimations(nil,yPosition: self.view.frame.size.height)
    ViewUtil.addShadowToView(commentFormLauncherButton,position: CGSize(width: 0,height: 5))

    UIView.commitAnimations()

}

按钮的动作是

@IBACtion func showCommentForm(_ sender: UIButton) {

    self.commenTinput.becomeFirstResponder()

}

日志(请参阅Gif)显示视图从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

@H_616_50@解决方法
好的,这是一个不需要CocoaPods的解决方案.

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

@IBOutlet weak var textFieldBottomcatonsTraint: NSLayoutConsTraint!

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

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

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

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

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

请注意,textFieldBottomcatonsTraint是底部约束的引用变量.

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

希望这有所帮助,并毫不犹豫地要求澄清!

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

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

大佬总结

以上是大佬教程为你收集整理的ios – 键盘可见时更改Y原点时的奇怪UIView行为全部内容,希望文章能够帮你解决ios – 键盘可见时更改Y原点时的奇怪UIView行为所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。