The keyboard hides when I click search or when I click on cancel.
But I want also that the keyboard hides when I click somewhere on the screen.
I found several tutorials for the textfield, but we are using the search bar.
Can someone tell me how to do this?
Thanks.
Try This
in your .h file add UISearchBar
#property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
in your .m file
- (void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
- (void) dismissKeyboard
{
// add self
[self.searchBar resignFirstResponder];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[yourTextField1 resignFirstResponder];
[yourTextField2 resignFirstResponder];
[yourTextField3 resignFirstResponder];
[yourSearchBar resignFirstResponder];
//etc
}
But probably you need to check where are you touching at since you don't want to hide the keyboard if you're touching on a text input or search box.
Try to use this one
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
You could add a UITapGestureRecognizer to dismiss your keyboard.
- (void)viewDidLoad {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
- (void) dismissKeyboard {
[self.view endEditing:YES];
}
Related
I have some textfield in which when i click it shows me keyboard , but when i click anywhere on the page the keyboard do not disappears . This is my code for resign of first responder. Whats wrong in my code.
Add this code:
-(void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapReceived:)];
[tapGestureRecognizer setDelegate:self];
[self.view addGestureRecognizer:tapGestureRecognizer];
}
-(void)tapReceived:(UITapGestureRecognizer *)tapGestureRecognizer
{
[textField resignFirstResponder];
}
Create iVar for UIGestureRecognizer
UIGestureRecognizer *tapper;
- (void)viewDidLoad
{
[super viewDidLoad];
tapper = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleTap:)];
tapper.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapper];
}
Dismiss what ever is currently editing:
- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
}
Try this code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[aTextField resignFirstResponder];
}
}
Or
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[self.view endEditing:YES];
}
}
- (void)viewDidLoad {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard {
[yourtextfield resignFirstResponder];
}
Add Tap Gesture on your Top View
you you havn't any subview which may hide your Root view
than only use self.view
if you are using ScrollView than add TapGesture on your ScrollView
-(void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDissmiss:)];
[self.view addGestureRecognizer: tap];
}
Handle Tap Gesture
-(void)tapDissmiss:(UITapGestureRecognizer *)tapGestureRecognizer
{
[self.view endEditing:YES];
}
Create one UITapGestureRecognizer as a class variable.
#interface ViewController() {
UITapGestureRecognizer *tapGesture;
}
- (void)viewDidLoad {
[super viewDidLoad]
tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(dismissKeyboard)];
}
Add gesture on textfield delegates textFieldDidBeginEditing, And remove it on textFieldDidEndEditing.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[self.view addGestureRecognizer:_tapGesture];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[self.view removeGestureRecognizer:_tapGesture];
}
Implement tap gesture selection
- (void)dismissKeyboard {
[self.view endEditing:YES];
}
I am new in iOS development. I want to hide the keyboard when tapping outside of a TextField. My TextField is in a cell from an UITableView.
I have tried to follow some of those links, however without any success--
Dismiss keyboard on touch anywhere outside UITextField
Dismiss keyboard by touching background of UITableView
Hide keyboard when scroll UITableView
I am trying to find the simplest way possible.
Thanks in advance
This is the simplest way to dismiss keyboard
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
[tableView addGestureRecognizer:gestureRecognizer];
}
- (void)hideKeyboard
{
[self.view endEditing:YES];
}
It's not about touch, only working when scroll
TableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
also have
UIScrollViewKeyboardDismissModeInteractive // the keyboard follows the dragging touch off screen, and may be pulled upward again to cancel the dismiss
Add delegate class UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
You'll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the textfield on it's selector.
you can use Tap gesture to hide keyboard.
- (void) tapGesture : (UIGestureRecognizer *) gestureRecognizer {
for (UIView *subview in view.subviews) {
if([subview isKindOfClass : [UITextField class]] ) {
UITextField *tf = (UITextField *) subview;
[tf resignFirstResponder];
}
}
}
Try This code
Write following code in viewDidLoad and add UIGestureRecognizerDelegate in .h file.
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[singleFingerTap setDelegate:self];
[self.view addGestureRecognizer:singleFingerTap];
// Listen for keyboard appearances and disappearances
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
Delegates of keyboard Appearances and disappearances
- (void)keyboardDidShow: (NSNotification *) notif{
// Do something here
tblview.tag = 1;
}
- (void)keyboardDidHide: (NSNotification *) notif{
// Do something here
tblview.tag = 0;
}
UITapGestureRecognizer event function for hide keyboard
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
blview.tag = 0;
[self.view endEditing:YES];
}
UIGestureRecognizer delegate
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if(tblview.tag == 1){
return TRUE;
}
else{
return FALSE;
}
}
I am using the solution in two parts:
To dismiss keyboard on tableview/collectionview tap:
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
gestureRecognizer.cancelsTouchesInView= NO;
[self.collectionView addGestureRecognizer:gestureRecognizer];
(Don't forget cancelsTouchesInView set to NO to get touch event of tableview/collection view)
To dismiss keyboard on scroll (as tableview/collectionview are subclass of UIScrollView):
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[self.view endEditing:YES];
}
Hope it helps somebody.
This will help you..
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
The easiest way is to alloc a tap Gesture in viewDidLoad and then hide keyboard
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
[_tableView addGestureRecognizer:gestureRecognizer];
}
- (void)hideKeyboard
{
[self.view endEditing:YES];
}
Or on github you certainly found a library that hide your keyboard
I want to hide the keyboard by touching the view. Everybody recommends to use this method, saying there's no need to link or anything else, but is not working.
The problem is that my method is not called.Is there anything else that should be done?
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:YES];
}
I had trouble with this so use a method that loops through all views seeing if they are textviews and firstResponders. Not sure of the structure of a UITextView but you might need to check for that too although it may be covered.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView *txt in self.view.subviews){
if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
[txt resignFirstResponder];
}
}
}
The best approach is to create a "lock view" which is a UIView that takes over the whole screen once the textField becomesFirstResponder. Make sure it's on top of all views (well, besides the textview, of course).
- (void)loadLockView {
CGRect bounds = [UIScreen mainScreen].bounds;
_lockView = [[UIView alloc] initWithFrame:bounds];
_lockView.backgroundColor = [UIColor clearColor];
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(lockViewTapped:)];
[_lockView addGestureRecognizer:tgr];
[self.view addSubview:_lockView];
}
- (void)lockViewTapped:(UITapGestureRecognizer *)tgr {
[_lockView removeFromSuperView];
[_textField resignFirstResponder];
}
Use UITapGestureRecognizer For Dismiss keyboard.
Write this code on your viewdidload().
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];[self.view addGestureRecognizer:tap];
and in dismissKeyboard method put this code.
-(void)dismissKeyboard
{
[TextFieldName resignFirstResponder];
}
I want to takeout the keyboard when the user clicks on the background or any other item on the view.
I found out that the following code will take it off. But where should i add it.
[self.view endEditing:YES];
All my UI components are created programatically.
It's super simple, you only need to implement this code in your .m file:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
This will do.
Use below code
UITapGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
recognizer.numberOfTouchesRequired=1;
[self.view addGestureRecognizer:recognizer];
recognizer.delegate = self;
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
[userName resignFirstResponder];
[passWord resignFirstResponder];
return NO;
}
I'm trying to hide the keyboard after a touch anywhere else on the screen. The code I'm using is based on this answer here.
IBOutlet UITextView *myTextView;
And the method:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([myTextView isFirstResponder] && [touch view] != myTextView) {
[myTextView resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
What I don't understand is how I should link my UITextField to the touchesBegan method. Which sent event do I need to use? Also, shouldn't the method be an IBAction, because right now I can't connect my UITextField to it.
I also gave this code a try but that one was breaking my navigation buttons (even with the solution mentioned in the comments)
Objective C:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:YES];
}
Swift:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
This is the best way I have found and it is very simple.
how I should link my UITextField to the touchesBegan method. Which sent event do I need to use? Also, shouldn't the method be an IBAction, because right now I can't connect my UITextField to it.
Because you don't. You have to override this method on the view of which the text field is a subview.
What I do, is change the overall UIView class to UIControl.
This gives you a touchDown event you can link up to a method to resignFirstResponder.
The UIControl still gives you all the functionality of a UIView.
-(IBAction)backgroundTap:(id)sender
{
[text1 resignFirstResponder];
[text2 resignFirstResponder];
[textLogin resignFirstResponder];
[textPassword resignFirstResponder];
} // Resign all responders
- (void)viewDidLoad
{
//for keybord hide when touch outside:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(hidekeybord)];
[self.view addGestureRecognizer:tap];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(void)hidekeybord
{
[_yourtextfield.txt resignFirstResponder];
}
In .h
#property (nonatomic, assign) id currentResponder;
In .h
//in viewDidLoad:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(resignOnTap:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:singleTap];
//Implement the below delegate method:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.currentResponder = textField;
}
//Implement resignOnTap:
- (void)resignOnTap:(id)iSender {
[self.currentResponder resignFirstResponder];
}
Try the quick and dirty way:
Take a button and link it to an action method(lets call it Background). Stretch the button so it covers the whole view. Adjust the layers of the views so only things the user interacts by touch are on top of the button. Change the type of button to custom, this make the button invisible. Dismiss the firstResponder in the method Background.