Question

I am writing an application that has a UITextView which allows editing. When a user first touches UITextView, a keyboard shows up and I want to retract that keyboard when user again touches the UITextView e.g. I have entered some data in a textview and with keyboard still showing on the screen I tap on the UITextView which should cause the keyboard to retract.

Is there any way to achive this?

(I'm aware of providing a done button and doing this but I want to achive this by tapping on UITextView itself)

Was it helpful?

Solution

As an aside, I would urge you not to use toggle state elements on the iPhone. It's to easy to double tap in real world use. That is why the Apple apps all use the either the "return" key on the keyboard or the done button.

In the interface you contemplate, the users will find themselves closing and then accidentally reopening the keyboard about 10% of the time or more. It will make your app feel cumbersome and flaky.

OTHER TIPS

You should call resignFirstResponder for the UITextView. Let's say you have an IBOutlet for the text view:

@property (nonatomic, retain) IBOutlet UITextView *comment;

Then [comment resignFirstResponder]; can be called from a touchesBegan or the like.

See e.g. How to Dismiss the Keyboard when using a UITextView.

What you are looking for is a large transparent uibutton "overlapButton" which always stays on top of the uitextview.

When the textview appears, you set the button hidden so you can tap on the textview freely.

[overlapButton setHidden:YES];

When tapping the textview, the keyboard will come up and the following method inside your textview delegate will get called:

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    [overlapButton setHidden:NO];
}

Here, you need to set visible the "overlapButton" so that while the keyboard is up, you can touch the button which now overlaps the textview. On the button action, you can hide the keyboard:

-(IBAction) overlapButtonTapped{
    [myTextView resignFirstResponder];
}

After resigning the first responder, the following method will get called:

-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
    [overlapButton setHidden:YES];
}

After setting the hidden property accordingly for the button (like above), you have a "clear" textview again which you can tap again to show the keyboard.. etc .. etc ..

Cheers.

I agree with you TechZen. I've seen this happen! I would not advise this also! It also makes editing very hard if not impossible for edit/copy/paste gestures. On the other hand, if the man still wants this badly.. :P the code provided by me in my other post works a treat! Cheers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow