dismissing the keyboard from a UITextField,UITextView as a subview of UIScrollView?

前端 未结 4 2087
野趣味
野趣味 2020-12-18 12:32

I have an application with UIScrollView added as a subview of UIView. i have added UITextField,UITextview as a subView of

相关标签:
4条回答
  • 2020-12-18 13:09

    Try this,

    - (void)viewDidLoad
    {
       [super viewDidLoad];
    
          UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
          tapGesture.cancelsTouchesInView = NO;
          [scrollView addGestureRecognizer:tapGesture];
          [tapGesture release];
    }
    -(void)dismissKeyboard
    {
        [txtNotes resignFirstResponder];
        [textView resignFirstResponder];
    }
    
    0 讨论(0)
  • 2020-12-18 13:22

    Just add UITapGestureRecognizer

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
        [scr addGestureRecognizer:singleTap];
    }
    
    
    - (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
    {
        //Get touch point
        CGPoint touchPoint=[gesture locationInView:scr];
    
        //Hide keyBoard
        [self.view endEditing:YES];
    }
    
    0 讨论(0)
  • 2020-12-18 13:22

    In iOS 7, you can achieve this easily.

    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
    
    0 讨论(0)
  • 2020-12-18 13:33

    When I added the gesture to a subclass of UIScrollView, I was having problems with the various gestures in my view tree interfering with each other, such as being able to click on subviews, scroll the view, and have the keyboard dismiss in all cases. I came up with this solution, which can be setup from a superclass of UIScrollView or from a UIViewController.

    The DismissKeyboardTapGesture class uses ARC, works with any text fields under the view, and doesn't take over any clicks from subviews like buttons. Also takes advantage of iOS7 scrolling effect to dismiss keyboard.

    Setting up from UISScrollView superclass:

        _dismissKeyboard = [[DismissKeyboardTapGesture alloc] initWithView:self];
    

    or from UIViewController:

        _dismissKeyboard = [[DismissKeyboardTapGesture alloc] initWithView:self.view];
    

    Here is the class:

    @interface DismissKeyboardTapGesture : NSObject <UIGestureRecognizerDelegate>
    
    @end
    
    @implementation DismissKeyboardTapGesture
    
    - (id)initWithView:(UIView *)view
    {
        self = [super init];
        if (self) {
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
            singleTap.cancelsTouchesInView = NO;
            singleTap.delegate = self;
            [view addGestureRecognizer:singleTap];
    
            if ([view respondsToSelector:@selector(setKeyboardDismissMode:)]) {
                // Bonus effect to dismiss keyboard by scrolling
                ((UIScrollView *)view).keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
            }
        }
        return self;
    }
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        // Don't stop any existing gestures in our view from working
        if (otherGestureRecognizer.view == gestureRecognizer.view) {
            return YES;
        }
        return NO;
    }
    
    - (void)singleTap:(UIGestureRecognizer*)gestureRecognizer
    {
        // Close keyboard for any text edit views that are children of the main view
        [gestureRecognizer.view endEditing:YES];
    }
    
    @end
    
    0 讨论(0)
提交回复
热议问题