Hiding the Keyboard when losing focus on a UITextView

后端 未结 10 558
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 10:34

So I have a UITextView that I\'m using to allow the user to submit some text.

My problem is, I can\'t seem to figure out how to allow the user to \'Cancel\' by tappi

10条回答
  •  心在旅途
    2020-12-04 10:37

    Another approach I used a lot when working with UITableViews, Searchbar and keyboard is this, I think you can apply it to text field

    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.tableView addGestureRecognizer:gestureRecognizer];
    gestureRecognizer.cancelsTouchesInView = NO;  // this prevents the gesture recognizers to 'block' touches
    [gestureRecognizer release];
    

    And then, here's the simple callback function

    - (void)hideKeyboard {
        [myTextField resignFirstResponder];
    }
    

    Hope it helps

提交回复
热议问题