When dismissing keyboard how do I keep my existing

2019-08-12 15:27发布

问题:

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.

Backtrace:

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:

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];

And these are the selectors:

- (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:

You don't need a gesture recognizer.

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