Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 546 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to dismiss keyboard for UITextView with return key?

#1
In IB's library, the introduction tells us that when the <kbd>return</kbd> key is pressed, the keyboard for `UITextView` will disappear. But actually the <kbd>return</kbd> key can only act as '\n'.

I can add a button and use `[txtView resignFirstResponder]` to hide the keyboard.

But is there a way to add the action for the <kbd>return</kbd> key in keyboard so that I needn't add `UIButton`?
Reply

#2
<pre><code>-(BOOL)textFieldShouldReturn:(UITextField *)textField; // called from textfield (keyboard)

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text; // good tester function - thanks</code></pre>
Reply

#3
Just like matt comment to samvermette, I don't like the idea of detecting "\n" either. The "return" key is there for a reason in UITextView, that is to go to next line of course.

The best solution in my opinion is to mimic iPhone message app - which is to add toolbar (and button) on the keyboard.

I got code from following blog post:


[To see links please register here]



Steps:

-Add toolbar to your XIB file - set the height to 460

-Add toolbar button item (if not already added). If you need to right-align it, also add flexible bar button item to XIB, and move the toolbar button item

-Create action that link your button item to resignFirstResponder as follow:

- (IBAction)hideKeyboard:(id)sender {
[yourUITextView resignFirstResponder];
}

-Then:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];

CGRect frame = self.keyboardToolbar.frame;
frame.origin.y = self.view.frame.size.height - 260.0;
self.keyboardToolbar.frame = frame;

[UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)notification {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];

CGRect frame = self.keyboardToolbar.frame;
frame.origin.y = self.view.frame.size.height;
self.keyboardToolbar.frame = frame;

[UIView commitAnimations];
}


Reply

#4
`UITextView` does not have any methods which will be called when the user hits the return key. If you want the user to be able to add only one line of text, use a `UITextField`. Hitting the return and hiding the keyboard for a `UITextView` does not follow the interface guidelines.

Even then if you want to do this, implement the `textView:shouldChangeTextInRange:replacementText:` method of `UITextViewDelegate` and in that check if the replacement text is `\n`, hide the keyboard.

There might be other ways but I am not aware of any.
Reply

#5
A more elegant way is to dismiss the keyboard when the user taps somewhere outside of the keyboard's frame.

First, set your ViewController's view to the class "UIControl" in the identity inspector in UIBuilder. Control-drag the view into the ViewController's header file and link it as an action with the event as Touch Up Inside, such as:

ViewController.h

-(IBAction)dismissKeyboardOnTap:(id)sender;

In the main ViewController file, ViewController.m:

-(IBAction)dismissKeyboardOnTap:(id)sender
{
[[self view] endEditing:YES];
}

You can require a double tap or long touch using similar techniques. You may need to set your ViewController to be a UITextViewDelegate and connect the TextView to the ViewController. This method works for both UITextView and UITextField.

Source: Big Nerd Ranch

EDIT: I'd also like to add that if you are using a UIScrollView, the above technique may not work as easily through the Interface Builder. In that case, you could use a UIGestureRecognizer and call the [[self view] endEditing:YES] method within it instead. An example would be:

-(void)ViewDidLoad{
....
UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(tap:)];
[self.view addGestureRecognizer: tapRec];
....
}

-(void)tap:(UITapGestureRecognizer *)tapRec{
[[self view] endEditing: YES];
}

When the user taps outside of the keyboard and does not tap an entry space, the keyboard will dismiss.
Reply

#6
I know it's not the exact answer to this question, but I found this thread after hunting the internet down for an answer. I assume others share that feeling.

This is my variance of the UITapGestureRecognizer which I find reliable and easy to use - just set the delegate of the TextView to the ViewController.

Instead of ViewDidLoad I add the UITapGestureRecognizer when the TextView becomes active for editing:


-(void)textViewDidBeginEditing:(UITextView *)textView{
_tapRec = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];

[self.view addGestureRecognizer: _tapRec];
NSLog(@"TextView Did begin");
}

When I tap outside the TextView, the view ends editing mode and the UITapGestureRecognizer removes itself so I can continue interacting with other controls in the view.

-(void)tap:(UITapGestureRecognizer *)tapRec{
[[self view] endEditing: YES];
[self.view removeGestureRecognizer:tapRec];
NSLog(@"Tap recognized, tapRec getting removed");
}

I hope this helps. It seems so obvious but I have never seen this solution anywhere on the web - am I doing something wrong?
Reply

#7
My hack for this :

1- create a button covering the whole view;
2- send it to the background of your view,
3- change it´s Type from "Round Rect" to "Custom" in the Attribute Inspector,
4- create an action
5- implement the action method:

- (IBAction)bgTouched:(id)sender
{
//to dismiss keyboard on bg btn pressed
[_userInput resignFirstResponder];
}
where _userInput is your TextField outlet

Reply

#8
Try this :


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@"\n"]) {
[self.view endEditing:YES];
}

return YES;

}
Reply

#9
You can also hide keyboard when touch in view screen:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[txtDetail resignFirstResponder];
}
}
Reply

#10
Just solved this problem a different way.

- Create a button that will be placed in the background
- From the Attribute Inspector, change the button type to custom, and the makes the button transparent.
- Expand the button to cover the whole view, and make sure the button is behind all the other object. Easy way to do this is to drag the button to the top of list view in the View
- Control drag the button to the `viewController.h` file and create an action (Sent Event: Touch Up Inside) like :

(IBAction)ExitKeyboard:(id)sender;

- In `ViewController.m` should look like :

(IBAction)ExitKeyboard:(id)sender {
[self.view endEditing:TRUE];
}
- Run app, and when you click away from the TextView, the keyboard disappears
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through