我有一个包含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 inputMoving up... 568.0 to 282.0// Tap anywhereMoving down... 282.0 to 568.0// Click again on button to focus the inputMoving up... 568.0 to 282.0///// Write "hhh". The vIEw is gone,I don't kNow why.// Tap anywhereMoving down... 568.0 to 568.0 // The vIEw seems to move from 568 to 568,hugh// Click on button to focus the inputMoving up... 568.0 to 282.0 // This dIDn't work apparently// Tap anywhereMoving down... 568.0 to 568.0 // 568 to 568 again
我错过了什么?
系统:iOS 9,Xcode 8,Swift 3
解决方法 好的,这是一个不需要CocoaPods的解决方案.我的方法涉及在键盘出现和消失时设置textFIEld的底部约束的常量.这意味着您需要创建对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,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 textFIEldBottomConstraint.constant = isKeyboardShowing ? keyboardFrame!.height + 4 : 4 self.vIEw.layoutIfNeeded() }}
请注意,textFIEldBottomConstraint是底部约束的引用变量.
这样做会使textFIEld在键盘出现或消失时设置它的底部约束,并且不会遇到您遇到的奇怪故障.
希望这有所帮助,并毫不犹豫地要求澄清!
YouTuber的功劳让我们构建该应用程序!通过此视频提供解决方案:
https://www.youtube.com/watch?v=p8IaS5lmhuM
总结以上是内存溢出为你收集整理的ios – 键盘可见时更改Y原点时的奇怪UIView行为全部内容,希望文章能够帮你解决ios – 键盘可见时更改Y原点时的奇怪UIView行为所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)