Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide keyboard after a push on an UINavigationBar back button on ios

My keyboard appears with a textView, I want to hide it when the user push on a back button on a navigation bar.

I have tried this:

-(void)viewWillDisappear:(BOOL)animated{

    [myTextView resignFirstResponder];
}

and this:

-(void)viewDidDisappear:(BOOL)animated{

    [myTextView resignFirstResponder];
}

But it doesn't work, how can I do this?

edit:

I found the solution here:

iPad keyboard will not dismiss if modal ViewController presentation style is UIModalPresentationFormSheet

like image 894
Anthony Avatar asked Apr 19 '12 12:04

Anthony


People also ask

How can hide back button in iOS?

Way 1: Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.

How do I hide the keyboard in Swift?

Microsoft SwiftKey does not have a dedicated minimize keyboard button. Instead, if you slide a finger down the keys from top to bottom, your Microsoft SwiftKey Keyboard is minimized.


2 Answers

Put this into the buttonPress method -

[self.view.window endEditing:YES];

Edit - this also lets you get the contents of the text being edited when the "back" button is pressed

like image 101
SomaMan Avatar answered Oct 06 '22 01:10

SomaMan


Combining the above answers and checking for back button will be done by this

- (void)viewWillDisappear:(BOOL)animated{
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
    // back button was pressed.  We know this is true because self is no longer
    // in the navigation stack.
    [self.view.window endEditing:YES];
}

[super viewWillDisappear:animated];

}

like image 34
megha Avatar answered Oct 06 '22 00:10

megha