解雇键盘时,如何将现有文本字段保留在同一位置? [英] When dismissing keyboard how do I keep my existing text fields in the same location?

查看:84
本文介绍了解雇键盘时,如何将现有文本字段保留在同一位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个包含UITextField和UILabel的UIScrollView,并且当用户在UITextField中单击时,键盘将显示.但是我需要一种允许用户关闭键盘的方法,而我试图使用向下滑动手势来完成操作(如在消息"中一样).我将手势识别器"添加到情节提要中的视图中,并将动作"添加到头文件中.方法如下:

So I have a UIScrollView that contains UITextFields and UILabels, and when the user clicks inside a UITextField the keyboard shows. But I need a way to allow the user to dismiss the keyboard and I was try to use the swipe down gesture to do it (as in Messages). I added the Gesture Recognizer to my view in the storyboard and added the Action to the header file. Here is the method:

- (IBAction)SwipeKeyboardDown:(id)sender
{
    if(self.moveTextField)
    {
        [self.moveTextField resignFirstResponder];
    } 
}

当用户单击第一个文本字段时,以及当他们尝试向下滑动所有文本字段并将标签移到屏幕底部时,我只需要进行测试.我需要做些什么来保持文本字段和标签的位置吗?我正在使用自动版式",并在情节提要中创建了它们.

I just have the if there for testing when a user has clicked on the first text field, and when they try to swipe down all of the text field and labels move to the bottom of the screen. Is there anything I need to do to keep the text fields and labels in their place? I am using Auto Layout and I created them all in the storyboard.

这是向下滑动键盘之前我的视图的外观:

This is how my view looks before swiping the keyboard down:

这是向下滑动后的样子:

And this is how it looks after swiping down:

感谢您的帮助,如果需要更多信息,请询问,我可以提供更多信息.

Thanks for the help, and if more info is needed please ask and I can provide more.

回溯:

2014-02-02 01:28:33.453 BJJDrillingAppPro[512:70b] (
    0   BJJDrillingAppPro                   0x0001de82 -[AddMoveViewControllerPro observeValueForKeyPath:ofObject:change:context:] + 210
    1   Foundation                          0x0116e8c7 NSKeyValueNotifyObserver + 362
    2   Foundation                          0x01170206 NSKeyValueDidChange + 458
    3   Foundation                          0x0112c8dd -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 120
    4   Foundation                          0x010ffcc7 _NSSetPointValueAndNotify + 185
    5   UIKit                               0x00320cae -[UIScrollView(UIScrollViewInternal) _adjustContentOffsetIfNecessary] + 2622
    6   UIKit                               0x0030384d -[UIScrollView setContentSize:] + 354
    7   BJJDrillingAppPro                   0x0002023d -[AddMoveViewControllerPro keyboardDidHide] + 493
    8   Foundation                          0x011f2bf9 __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke + 40
    9   CoreFoundation                      0x017f8524 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
    10  CoreFoundation                      0x0175000b _CFXNotificationPost + 2859
    11  Foundation                          0x0112c951 -[NSNotificationCenter postNotificationName:object:userInfo:] + 98
    12  UIKit                               0x0071d6f5 -[UIInputViewTransition postNotificationsForTransitionStart] + 1004
    13  UIKit                               0x007137e2 -[UIPeripheralHost(UIKitInternal) executeTransition:] + 592
    14  UIKit                               0x00715c0e -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] + 1029
    15  UIKit                               0x00716019 -[UIPeripheralHost(UIKitInternal) setInputViews:animated:] + 72
    16  UIKit                               0x00716063 -[UIPeripheralHost(UIKitInternal) setInputViews:] + 67
    17  UIKit                               0x0070d2fa -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:] + 1453
    18  UIKit                               0x003e707c -[UIResponder _finishResignFirstResponder] + 163
    19  UIKit                               0x003e719f -[UIResponder resignFirstResponder] + 265
    20  UIKit                               0x0096b5f4 -[UITextField resignFirstResponder] + 118
    21  BJJDrillingAppPro                   0x0001dd70 -[AddMoveViewControllerPro SwipeKeyboardDown:] + 288
    22  UIKit                               0x00605e8c _UIGestureRecognizerSendActions + 230
    23  UIKit                               0x00604b00 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 383
    24  UIKit                               0x0060656d -[UIGestureRecognizer _delayedUpdateGesture] + 60
    25  UIKit                               0x00609acd ___UIGestureRecognizerUpdate_block_invoke + 57
    26  UIKit                               0x00609a4e _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 317
    27  UIKit                               0x00600148 _UIGestureRecognizerUpdate + 199
    28  UIKit                               0x002cc19a -[UIWindow _sendGesturesForEvent:] + 1291
    29  UIKit                               0x002cd0ba -[UIWindow sendEvent:] + 1030
    30  UIKit                               0x002a0e86 -[UIApplication sendEvent:] + 242
    31  UIKit                               0x0028b18f _UIApplicationHandleEventQueue + 11421
    32  CoreFoundation                      0x0172583f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    33  CoreFoundation                      0x017251cb __CFRunLoopDoSources0 + 235
    34  CoreFoundation                      0x0174229e __CFRunLoopRun + 910
    35  CoreFoundation                      0x01741ac3 CFRunLoopRunSpecific + 467
    36  CoreFoundation                      0x017418db CFRunLoopRunInMode + 123
    37  GraphicsServices                    0x03aa39e2 GSEventRunModal + 192
    38  GraphicsServices                    0x03aa3809 GSEventRun + 104
    39  UIKit                               0x0028dd3b UIApplicationMain + 1225
    40  BJJDrillingAppPro                   0x0001bf0d main + 141
    41  libdyld.dylib                       0x0213c70d start + 1
    42  ???                                 0x00000001 0x0 + 1
)

在查看了我的键盘代码后,显示和隐藏了方法之后,我想知道是否在这里做错了事:

And after looking at my code for the keyboard did show and hide methods I am wondering if maybe I am doing something wrong there:

这在我的viewDidLoad中:

This is in my viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardDidShow)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardDidHide)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];

这些是选择器:

- (void)keyboardDidHide
{
    if(keyboardShown)
    {
        CGRect r = self.scrollView.frame;
        r.size.height += 216;
        self.scrollView.frame = r;

        keyboardShown = NO;
    }
}

- (void)keyboardDidShow
{
    if(!keyboardShown)
    {
        CGRect r = self.scrollView.frame;
        self.scrollView.contentSize = self.scrollView.frame.size;
        r.size.height -= 216;
        self.scrollView.frame = r;

        keyboardShown = YES;
    }
}

推荐答案

您不需要手势识别器.

滚动视图具有keyboardDismissMode属性.将其设置为UIScrollViewKeyboardDismissModeOnDragUIScrollViewKeyboardDismissModeInteractive即可进入.

Scrollviews have the keyboardDismissMode property. Set it to UIScrollViewKeyboardDismissModeOnDrag or UIScrollViewKeyboardDismissModeInteractive and you are set to go.

这篇关于解雇键盘时,如何将现有文本字段保留在同一位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆