解除键盘时如何将现有文本字段保留在同一位置?

时间:2014-01-31 21:05:09

标签: ios iphone uiscrollview keyboard uitextfield

所以我有一个包含UITextFields和UILabels的UIScrollView,当用户点击UITextField时,键盘会显示。但我需要一种方法来允许用户关闭键盘,我尝试使用向下滑动手势来完成它(如在消息中)。我在故事板中添加了Gesture Recognizer到我的视图,并将Action添加到头文件中。这是方法:

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

当用户点击第一个文本字段时,我只有用于测试,当他们尝试向下滑动所有文本字段时,标签移动到屏幕底部。有什么我需要做的就是保持文本字段和标签的位置吗?我正在使用自动布局,我在故事板中创建了它们。

这是在向下滑动键盘之前我的视图的外观: How it looks before swiping keyboard down

这就是向下滑动后的样子: enter image description here

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

回溯:

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
)

在查看我的键盘代码后,确实显示并隐藏了方法,我想知道是否可能在那里做错了什么:

这是在我的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;
    }
}

1 个答案:

答案 0 :(得分:1)

您不需要手势识别器。

Scrollviews具有keyboardDismissMode属性。将其设置为UIScrollViewKeyboardDismissModeOnDragUIScrollViewKeyboardDismissModeInteractive,您就可以了。