ios – Swift,移动键盘后面的内容在关闭后不会重置

ios – Swift,移动键盘后面的内容在关闭后不会重置,第1张

概述我在我的一个VC中有一个uiscrollview.在scrollView中,我有多个TF,按钮等.我使用下面的代码,根据Apple文档,在调用键盘通知时向上移动scrollView,以推高隐藏的文本字段.但是,当我关闭键盘时,scrollView不会重置或向下移动,它只是停留在“向上移动”的位置. 我在这里错过了什么吗?我有一个名为的类的成员变量: var activeTextField: UIT 我在我的一个VC中有一个uiscrollvIEw.在scrollVIEw中,我有多个TF,按钮等.我使用下面的代码,根据Apple文档,在调用键盘通知时向上移动scrollVIEw,以推高隐藏的文本字段.但是,当我关闭键盘时,scrollVIEw不会重置或向下移动,它只是停留在“向上移动”的位置.

我在这里错过了什么吗?我有一个名为的类的成员变量:@H_403_3@

var activeTextFIEld: UITextFIEld!

我是否正确使用了委托方法?@H_403_3@

(func registerForKeyboardNotifications() {    let notificationCenter = NSNotificationCenter.defaultCenter()    notificationCenter.addobserver(self,selector: "keyboarDWillBeShown:",name: UIKeyboarDWillShowNotification,object: nil)    notificationCenter.addobserver(self,selector: "keyboarDWillBeHIDden:",name: UIKeyboarDWillHIDeNotification,object: nil)}func keyboarDWillBeShown(sender: NSNotification) {    let info: NSDictionary = sender.userInfo!    let value: NSValue = info.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue    let keyboardSize: CGSize = value.CGRectValue().size    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0,0.0,keyboardSize.height,0.0)    scrollVIEw.contentInset = contentInsets    scrollVIEw.scrollindicatorInsets = contentInsets    // If active text fIEld is hIDden by keyboard,scroll it so it's visible    // Your app might not need or want this behavior.    var aRect: CGRect = self.vIEw.frame    aRect.size.height -= keyboardSize.height    let activeTextFIEldRect: CGRect? = activeTextFIEld?.frame    let activeTextFIEldOrigin: CGPoint? = activeTextFIEldRect?.origin    if (!CGRectContainsPoint(aRect,activeTextFIEldOrigin!)) {        scrollVIEw.scrollRectToVisible(activeTextFIEldRect!,animated:true)    }}// Called when the UIKeyboarDWillHIDeNotification is sentfunc keyboarDWillBeHIDden(sender: NSNotification) {    let contentInsets: UIEdgeInsets = UIEdgeInsetsZero    scrollVIEw.contentInset = contentInsets    scrollVIEw.scrollindicatorInsets = contentInsets}func textFIEldDIDBeginEditing(textFIEld: UITextFIEld!) {    activeTextFIEld = textFIEld    scrollVIEw.scrollEnabled = true}func textFIEldDIDEndEditing(textFIEld: UITextFIEld!) {    activeTextFIEld = nil    scrollVIEw.scrollEnabled = false}overrIDe func vIEwWillAppear(animated: Bool) {    super.vIEwWillAppear(animated)    self.registerForKeyboardNotifications()}overrIDe func vIEwDIDdisappear(animated: Bool) {    super.vIEwWilldisappear(animated)    NSNotificationCenter.defaultCenter().removeObserver(self)}
解决方法 我这样做了,它对我有用.似乎比你的方式容易多了.我为我的项目使用了UITextVIEw.但它应该与UITextFIEld一样. – / 25只是为了让标题显示出来.
func textVIEwDIDBeginEditing(textVIEw: UITextVIEw) {    myScrollVIEw.setContentOffset(CGPointMake(0,textVIEw.frame.origin.y-25),animated: true)}func textVIEwDIDEndEditing(textVIEw: UITextVIEw) {    myScrollVIEw.setContentOffset(CGPointMake(0,-textVIEw.frame.origin.y+25),animated: true)}
总结

以上是内存溢出为你收集整理的ios – Swift,移动键盘后面的内容在关闭后不会重置全部内容,希望文章能够帮你解决ios – Swift,移动键盘后面的内容在关闭后不会重置所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1100155.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存