Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best method I've seen for this is to add a transparent subview that overlays the text view and handles the TouchesBegan first. You can then detect touches outside the text field and dismiss the keyboard by having the text field resign as first responder.</p> <p>For example, create the overlay subview in IB or programmatically, either way. Place and size it so that it covers the text view(s), and give it a clear color. If you add the view via IB, hide it when your main view loads so that it doesn't absorb touches just yet, like so:</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; overView.hidden = YES; } </code></pre> <p>Then when the text field begins editing, unhide the view:</p> <pre><code>- (void)textFieldDidBeginEditing:(UITextField *)textField { overView.hidden = NO; } </code></pre> <p>When the text field ends editing, rehide the view:</p> <pre><code>- (void)textFieldDidEndEditing:(UITextField *)textField { overView.hidden = YES; } </code></pre> <p>Add a touchesBegan to detect when your unhidden overView is touched:</p> <pre><code>- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; // Resign first responder if touch is outside text field if ([touch view] != myTextField) [myTextField resignFirstResponder]; // Send touches up the responder chain (pass them through) [self.nextResponder touchesBegan:touches withEvent:event]; } </code></pre> <p>You can also do this through custom gestures, but that only works with iOS 4.x and, in my opinion, is more complicated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload