Hide the keybord on clicking enter button or search button - iphone

I've added the UISearchBar button inside the UIBarButtonItem inside the toolbar button.
In the below form:
// search bar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 260, 44)];
UIBarButtonItem *searchBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
[searchBar release];
[buttons addObject:searchBarButtonItem];
Now the problem in when I click on the UISearchBar keyboard appears. I'd like to hide the keyboard on clicking in enter or search button. How can i do this?

Implement a method from UISearchBarDelegate:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
}

For Swift
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}

You need to process the UISearchBarDelegate protocol methods.
In the following methods, call -resignFirstResponder method of UISearchBar.
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
The detail of above delegate methods can be found in the Apple official document. I often resign first responder inside those delegate methods as the end of searching.

you can use UISEarchBar's delegate method
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
and for enter (not tested)
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText; // called when text changes (including clear)
{
if ([searchText isEqualToString:#"\n"])
[searchBar resignFirstResponder];
}
Hope this helps you...

In the header file, add or connect to the UISearchBar outlet: should look something like this:
#property (weak, nonatomic) IBOutlet UISearchBar *searchBarName;
Then, in the implementor, add the following:
self.searchBarName.delegate = self;
in the viewDidLoad function.
Then add the following function:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.searchBarName resignFirstResponder];
}
Also as mentioned above ensure that this is added/extended in the header UISearchBarDelegate

use [textField endEditing:YES];, it should work for you, or you can create an oject of ur button and do [self.buttonObj resignFirstResponder]

Related

How i dismiss keyboard on tapping search button on keyboard regarding UISearchBar?

I m developing an application in which i adding one control UISearchBar. When i started editing text in UIsearchBar then keypad is animated on the screen. After i completed my complete editing or canceling all text then i will stuck on point of dismissing keypad.
How i dismiss keyboard on tapping search key form UIKeypad?
Also same question for UITextField and UITextView?
Thanks
Make sure the view controller which has the SearchBar implements the SearchBarDelegate and you set the searchBar.delegate to self:
#interface AddressSearchViewController : UIViewController <UISearchBarDelegate>
then implement the following method:
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
This will make the keyboard disappear when you tap the search button on the keyboard or the search bar
For the search bar:
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
And same thing for the text field:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
Use this code in ViewDidLoad
-(void) ViewDidLoad
{
[super ........];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
[self.view addGestureRecognizer:gestureRecognizer];
gestureRecognizer.cancelsTouchesInView = NO;
}
- (void) hideKeyboard
{
[texfieldname1 resignFirstResponder];
[texfieldname2 resignFirstResponder];
}

UITextView resign first responder on 'Done'

I have been searching around the web for about an hour now, and cannot find any code to help me with this.
I have a UITextView that I need to resign first responder of when the user presses the 'Done' button on their keyboard.
I have seen code floating around the internet like this:
-(BOOL) textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
But that will not work for a UITextView.
Simply put,
How can I tell when the user presses the done button on the keyboard?
Implement the shouldChangeTextInRange: delegate method.
Use below approach and the solution work only with #"\n" (new line character).
//In you *.h file make sure you add
#interface v1ViewController : UIViewController <UITextViewDelegate>
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
myTextField.delegate = self;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return NO;
}
// For any other character return TRUE so that the text gets added to the view
return YES;
}
You can use the delegate method
- (void)textViewDidEndEditing:(UITextView *)textView {
[textView resignFirstResponder];
}
You have to set your controller as the delegate for the TextView.
For more methods you can have a look here: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html%23//apple_ref/doc/uid/TP40006897

how can I call a method as I click UITextField?

I want to ask that how can I call a method as I do click(touch for start writing) in iphone UITextField, as like we click UIButton and put method in "addTarget", is there any way for UITextFields ?
implement a UITextFieldDelegate and do whatever you want to do in - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField or - (void)textFieldDidBeginEditing:(UITextField *)textField
You should use the first method if you want to stop the textfield from behaving like a textfield. For example if you want to open the textfield editor in a modal view. You can return NO there if you don't want this behavior.
Edit: Here is the code to call myMethod:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self myMethod];
return YES;
}
You can set textField's delegate and implement textFieldShouldBeginEditing: method in it - that method will get called when user taps the field and before it goes into editing.
See UITextFieldDelegate reference for more methods available.
[textField addTarget:self action:#selector(myMethod:) forControlEvents:UIControlEventEditingChanged];
-(void) myMethod:(id)sender
{
UITextField* textField = (UITextField *)sender;
}
i think this will help..!!
You can code as,
[YourTextField addTarget:self action:#selector(myMethod:) forControlEvents:UIControlEventEditingDidBegin];
-(void)myMethod
{
[YourTextField resignFirstResponder];
//Do whatever you want
}

Remove clear button (grey x) to the right of UISearchBar when cancel button tapped

Right, to begin my question, here's some screenies of the problem already solved by the Spotify app:
Spotify's Step 1: Standard UISearchBar not in editing mode.
Spotify's Step 2: UISearchBar now in editing mode. Search term entered. Cancel button slides in from the right, and the clear button (grey x) appears.
Spotify's Step 3: Cancel button pressed; keyboard slides out and the search bar is no longer in editing mode. Search term remains and the grey x button is now hidden.
At present, the following code fires off when my cancel button is pressed:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO animated:YES];
}
Which results in:
My Step 3: Search bar now not in editing mode. Cancel button and keyboard has slid out. Search term remains but so does the grey x.
So, my question is this: given that -resignFirstResponder (and -endEditing:, FYI) does not hide the grey x button when a search bar has had text entered into it, how does one hide it?
Thanks again, friends.
The problem is that UISearchBar doesn't expose it's text field, and manages the properties on the text field itself. Sometimes, the values of the properties aren't what you want.
For instance, in my own app, I wanted the keyboard style for my search bar to use the transparent alert style.
My solution was to walk through the subviews of the search bar until you find the text field. You should then be able to set the clearButtonMode property, using something like UITextFieldViewModeWhileEditing as a parameter.
This should make it so that the clear button is only shown while the text field is editing.
You want to do this on viewDidLoad or something early, so it's set before you start using it (but after the search bar is initialised.
for (UIView *subview in searchBar.subviews)
{
if ([subview conformsToProtocol:#protocol(UITextInputTraits)])
{
[(UITextField *)subview setClearButtonMode:UITextFieldViewModeWhileEditing];
}
}
Looks like iOS 7 changed the view hierarchy of UISearchBar, and the text box is deeper in the view (The above solution didn't work for me). However, modifying the above solution to traverse the whole hierarchy works:
[self configureSearchBarView:[self searchBar]];
- (void)configureSearchBarView:(UIView*)view {
for (UIView *subview in [view subviews]){
[self configureSearchBarView:subview];
}
if ([view conformsToProtocol:#protocol(UITextInputTraits)]) {
[(UITextField *)view setClearButtonMode:UITextFieldViewModeWhileEditing];
}
}
I'm building upon the previous answers because I started seeing crashes on iOS 7.1 unless I made the following change. I added an additional call to respondsToSelector for each view to make sure that setClearButtonMode: can be called. I observed an instance of UISearchBar getting passed in, which seems to conform to the UITextInputTraits protocol yet does not have the setClearButtonMode: selector, so a crash occurred. An instance of UISearchBarTextField also gets passed in and is the actual object for which to call setClearButtonMode:.
- (void)removeClearButtonFromView:(UIView *)view
{
if (!view)
{
return;
}
for (UIView *subview in view.subviews)
{
[self removeClearButtonFromView:subview];
}
if ([view conformsToProtocol:#protocol(UITextInputTraits)])
{
UITextField *textView = (UITextField *)view;
if ([textView respondsToSelector:#selector(setClearButtonMode:)])
{
[textView setClearButtonMode:UITextFieldViewModeNever];
}
}
}
You need to get the textField of the Search Bar
UITextField *textField = [searchBar valueForKey:#"_searchField"];
textField.clearButtonMode = UITextFieldViewModeNever;
use in - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar method.
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
UITextField *textField = [searchBar valueForKey:#"_searchField"];
textField.clearButtonMode = UITextFieldViewModeNever;
}
A better way to do this in iOS7 is:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setClearButtonMode:UITextFieldViewModeWhileEditing];
To expand on Jadariens answer if you never want the grey x to appear you need to use the following
for (UIView *subview in searchBar.subviews)
{
if ([subview conformsToProtocol:#protocol(UITextInputTraits)])
{
[(UITextField *)subview setClearButtonMode:UITextFieldViewModeNever];
}
}
Accepted answer does not work on iOS7+, here is the modified version as a Swift extension
extension UIView {
class func removeClearButton(svs: [UIView]) {
for sv in svs {
if let tv = sv as? UITextField where sv.conformsToProtocol(UITextInputTraits) {
tv.clearButtonMode = .Never
return
} else {
UIView.removeClearButton(sv.subviews)
}
}
}
}
Usage
UIView.removeClearButton(searchBar.subviews)
Hers is a category I wrote that does this
Category
#implementation UISearchBar (Additions)
- (void)setClearButtonMode:(UITextFieldViewMode)viewMode {
UITextField *textField = [self findTextFieldInView:self];
[textField setClearButtonMode:viewMode];
}
- (UITextField *)findTextFieldInView:(UIView *)view {
for (UIView *subview in view.subviews) {
if ([subview isKindOfClass:[UITextField class]] ||
[subview.class isSubclassOfClass:[UITextField class]]) {
return (UITextField *)subview;
}
UITextField *textField = [self findTextFieldInView:subview];
if (textField) {
return textField;
}
}
return nil;
}
#end
Usage
[searchBar setClearButtonMode:UITextFieldViewModeWhileEditing];
There's a better way than any of the answers here, and you don't have to use private APIs or traverse subviews to do it.
UISearchBar has a built-in API for doing this:
[UISearchBar setImage:forSearchBarIcon:state]
The SearchBar icon key you want is UISearchBarIconClear, and you want the UIControlStateNormal state. Then give it a clear image for the image, and you're done.
So, it should look like this:
[searchBar setImage:clearImage forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
For the (x) icon in searchBar. You can use below delegate method.
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = YES;
}
for (UIView *subview in _search_bar.subviews)
{
NSLog(#"%#",subview.subviews);
for (UIView *subview11 in subview.subviews)
{
if ([subview11 conformsToProtocol:#protocol(UITextInputTraits)])
{
[(UITextField *)subview11 setClearButtonMode:UITextFieldViewModeNever];
}
}
}

Disabling Keyboard

How to Hide Keyboard by pressing Returnkey
There is a couple of things you need to remember. The number #1 part developers forget to set is the delegate of the textField.
If you are using the Interface Builder, you must remember that you need to set the delegate of the textField to the file Owner.
alt text http://www.thoughtblog.com/imgs/delegate.png
If you are not using Interface Builder then make sure you set the delegate of the textfield to self. I also include the returnType. For Example if the textField was called gameField:
gameField.delegate = self;
gameField.returnType = UIReturnKeyDone;
You must also implement the UITextFieldDelegate for your ViewController.
#interface YourViewController : UIViewController <UITextFieldDelegate>
Finally you need to use the textFieldShouldReturn method and call [textField resignFirstResponder]
-(BOOL) textFieldShouldReturn:(UITextField*) textField {
[textField resignFirstResponder];
return YES;
}
All your textFields will use this same method so you only need to have this setup once. As long as the delegate is set for the textField, the UITextFieldDelegate is implemented for the interface, you add the textFieldShouldReturn method and call the
resignFirstResponder your set.
The keyboard only shows up when something editable (usually a UITextField) has become the first responder. Therefore, to make the keyboard go away, you have to make the textField not be the firstResponder anymore. Fortunately, it's one line of code:
[myTextField resignFirstResponder];
You really need to include more information with your question, but I think this might be what you are looking for:
First, make your view controller implement the UITextFieldDelegate:
#interface MainViewController : UIViewController <UITextFieldDelegate> {
Then add this method to the controller:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Read the UITextFieldDelegate documentation to see what else you can do.
Use these two methods:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
activeTextField = textField;
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
activeTextField = nil;
[txtPassword resignFirstResponder];
[txtUserName resignFirstResponder];
return YES;
}
Please make sure you have given delegates to each textfields. For that you should go to the view. Right click . set the delegate to view.