How to resign a UITextField of UISearchBar? I tried with the below code
UIView * subView;
NSArray * subViews = [self.searchBar subviews];
for(subView in subViews)
{
if( [subView isKindOfClass:[UITextField class]] )
{
((UITextField*)subView).delegate=self;
((UITextField*)subView).returnKeyType=UIReturnKeyDone;
break;
}
}
for (UIView *subview in self.searchBar.subviews)
{
if ([subview conformsToProtocol:#protocol(UITextInputTraits)])
{
[(UITextField *)subview setClearButtonMode:UITextFieldViewModeWhileEditing];
}
}
And added the TextField Delegate method
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
this works but this resigns the keyboard oly if the textfield is not empty. If the textfield is empty then it is not resigning
Use this :
[searchBar resignFirstResponder];
Please do this one...
-(void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
{
if (searchBar.text.length == 0)
[searchBar resignFirstResponder];
}
i hope this will work fine.
First set Delegate in .h file like bellow...
#interface yourViewController : UIViewController<UISearchBarDelegate>
and when you want to resign keyboard of UISearchBar simple use bellow line..
[searchBar resignFirstResponder];
UPDATE:
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
[self.searchBar resignFirstResponder];
return YES;
}
Related
I'm trying to hide the keyboard when the user selects a UITextField. I currently have three text fields: two UIPickerView (controlled by the same picker) and a textinput field. When I click "return" or on the background, the keyboard disappears via a resignFirstResponder call.
PROBLEM:
When I am currently editing the text input field and then immediately select the UITextField w/ UIPickerView functionality, the keyboard doesn't disappear. I feel like i've tried every solution and am beating by head against a wall...
CODE:
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[pickerView setHidden:YES];
if (fldQuiver.editing == YES) {
[fldTitle resignFirstResponder];
[fldQuiver resignFirstResponder];
[pickerView setHidden:NO];
variabla = 1;
}else if (fldCategory.editing == YES) {
[fldTitle resignFirstResponder];
[fldCategory resignFirstResponder];
[pickerView setHidden:NO];
variabla = 2;
}
NSLog(#"variabla %d",variabla);
[pickerView reloadAllComponents];
}
Any help would be appreciated. Thanks in advance.
EDITED CODE FOR PRINCE:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
{
//set logic for picker view here
if (textField == fldQuiver)
{
variabla = 1;
}
else if (textField == fldCategory) {
variabla = 2;
}
else
{
}
NSLog(#"variabla %d",variabla);
[pickerView reloadAllComponents];
if (textField == fldQuiver)
{
[fldTitle resignFirstResponder];
[pickerView setHidden:NO];
return NO;
}
else if (textField == fldCategory) {
[fldTitle resignFirstResponder];
[pickerView setHidden:NO];
return NO;
}
else
{
[pickerView setHidden:YES];
return YES;
}
}
ALSO: I have "synthesized" and declared #property fldQuiver and fldCategory. I have IBOutlet for fldTitle, fldQuiver, fldCategory.
The pickerView is loaded with an array based on variable.
Use textFieldShouldBeginEditing delegate method for this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
{
//set logic for picker view here
if (textField == fldQuiver)
{
//picker view hidden or show here
return NO;
}
else if (textField == fldCategory) {
//picker view hidden or show here
return NO;
}
else
{
return YES;
}
}
set delegate for text field
textField.delegate=self;
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
...........
[textField resignFirstResponder]; //it common for all text field,so not use multi resignFirstResponder
............
}
I have a UIScrollView in which inside of that I have a UITextField, so what I did is that on the third text field, which is password, I scrolled down the offset of the UIScrollView:
#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ((textField == self.emailTextField_ || textField == self.passwordTextField_)){
if (self.scrollView_.contentOffset.y != self.emailTextField_.frameY - self.emailTextField_.frameHeight/2){
shouldAdjustOffset = YES;
} else {
shouldAdjustOffset = NO;
}
if (shouldAdjustOffset){
[self.scrollView_ setContentOffset:CGPointMake(0, self.emailTextField_.frameY - self.emailTextField_.frameHeight/2) animated:YES];
}
}
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.firstNameTexField_){
[self.firstNameTexField_ resignFirstResponder];
[self.lastNameTextField_ becomeFirstResponder];
} else if (textField == self.lastNameTextField_){
[self.lastNameTextField_ resignFirstResponder];
[self.emailTextField_ becomeFirstResponder];
} else if (textField == self.emailTextField_){
[self.emailTextField_ resignFirstResponder];
[self.passwordTextField_ becomeFirstResponder];
} else if (textField == self.passwordTextField_){
[self signupButtonPressed:nil];
[self.passwordTextField_ resignFirstResponder];
}
return YES;
}
Now the issue is that when I am on the next text field it resets the content offset back to 0, when I do:
[self.emailTextField_ resignFirstResponder];
[self.passwordTextField_ becomeFirstResponder];
. How can I prevent this from happening?
Just turn of scrollview scroll before showing keypad (becomeFirstResponder) and enable back the scroll after it,
scrollView.scrollEnabled = NO;
[someControl becomeFirstResponder];
scrollView.scrollEnabled = YES;
[scrollview doTheScrollingHere];
#Deepan you saved me a lot of time :) Only what I'll suggest is to override UITextField method becomeFirstResponder to check first if it is in UIScrollView and if is disable scroll for a moment
- (BOOL)becomeFirstResponder
{
if ([self.superview isKindOfClass:[UIScrollView class]])
{
[(UIScrollView*)self.superview setScrollEnabled:NO];
}
BOOL accept = [super becomeFirstResponder];
if ([self.superview isKindOfClass:[UIScrollView class]])
{
[(UIScrollView*)self.superview setScrollEnabled:YES];
}
return accept
}
i have 6 text field.i want to get current cursor position(cursor in which text field).like android onfocus .after i get the position i need to draw slider near to that textfield. is there any API for iPhone.guide me i'm new to iPhone and Xcode.
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
Add your own logic for ‘for' loop here. I am using my logic. (As per my requirements).
NSArray *subviews = [[NSArray alloc] initWithArray:self.view.subviews];
for(UIView *subview in subviews)
if([subview isKindOfClass:[UITextField class]])
{
UITextField *textField = (UITextField *) subview;
if([textField isFirstResponder])
{
// do whatever you want
}
}
If You want to see TextField In center OF Screen hen Use
Firstly You Add ScrollView in nid and connect and all TextField's delegete is connect with fileOwner
and Implement this code
It"s Better according to your View
#pragma mark -
#pragma mark textField Delegte
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[self scrollViewToCenterOfScreen:textField];
return YES;
}
- (void)scrollViewToCenterOfScreen:(UIView *)theView {
CGFloat viewCenterY = theView.center.y;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGFloat availableHeight = applicationFrame.size.height - 200; // Remove area covered by keyboard
CGFloat y = viewCenterY - availableHeight / 2.0;
if (y < 0) {
y = 0;
}
[scrollview setContentOffset:CGPointMake(0, y) animated:YES];
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
if ([textField isEqual:textField1])
{
[textField2 becomeFirstResponder];
}
else if ([textField isEqual:textField2])
{
[textField3 becomeFirstResponder];
}
else if ([textField isEqual:textField3])
{
[textField4 becomeFirstResponder];
}
else
{
[textField4 resignFirstResponder];
[scrollview setContentOffset:CGPointMake(0, 0) animated:YES];
}
return YES;
}
I have a search functionality using UISearchBar that occurs on-the-fly, so I think it would be more obvious to replace that "Search" button on the keyboard with "Done".
Is there a way to do that?
thanks
You can change the keyboardType property of your UISearchBar object. However, there is not a way to change the returnKeyType directly. You may be able to filter down and change it manually. Check the documentation for UISearchBar and see if you can find returnKeyType as that is what you are looking for.
I accomplish it this way:
// -- Basic UISearchBar setup.
self.theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,38)];
[self.theSearchBar setDelegate:self];
[self.view addSubview:self.theSearchBar];
// -- Customize the returnKeyType of the search bar's nested UITextField.
UITextField *searchBarTextField = [[self.theSearchBar subviews] objectAtIndex:1];
searchBarTextField.returnKeyType = UIReturnKeyGo;
Hope that is helpful. This approach (i.e. grabbing a subview by index) may break in the future, but it works fine for now.
for (UIView *view in _searchBar.subviews){
if ([view isKindOfClass:[UITextField class] ]) {
UITextField *searchTf = (UITextField *)view;
searchTf.returnKeyType = UIReturnKeyDone;
}
}
This is working for iOS 6
UITextField *searchBarTextField = [[searchBarObj subviews] objectAtIndex:1];
searchBarTextField.returnKeyType = UIReturnKeyDefault;
[searchBarTextField setEnablesReturnKeyAutomatically:NO];
This is working for iOS 7
for (UIView *subview in self.searchBar.subviews)
{
for (UIView *subSubview in subview.subviews)
{
if ([subSubview conformsToProtocol:#protocol(UITextInputTraits)])
{
UITextField *textField = (UITextField *)subSubview;
[textField setKeyboardAppearance: UIKeyboardAppearanceAlert];
textField.returnKeyType = UIReturnKeyDone;
break;
}
}
}
Don't rely on it being the second subview, use isKindOfClass: method to check. It will be more iOS update proof that way.
for (UIView *subview in self.theSearchBar.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
[(UITextField *)subview setReturnKeyType:UIReturnKeyGo];
break;
}
}
In the UISearchBar control, is the a way to change the Search key title for the keyboard to Done?
For a searchbar named tablesearchbar:
// Set the return key and keyboard appearance of the search bar
for (UIView *searchBarSubview in [tableSearchBar subviews]) {
if ([searchBarSubview conformsToProtocol:#protocol(UITextInputTraits)]) {
#try {
[(UITextField *)searchBarSubview setReturnKeyType:UIReturnKeyDone];
[(UITextField *)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];
}
#catch (NSException * e) {
// ignore exception
}
}
}
At least for iOS 8, simply:
[self.searchBar setReturnKeyType:UIReturnKeyDone];
As of iOS 7 beta 5, Run Loop's answer didn't work for me, but this did:
for(UIView *subView in [searchBar subviews]) {
if([subView conformsToProtocol:#protocol(UITextInputTraits)]) {
[(UITextField *)subView setReturnKeyType: UIReturnKeyDone];
} else {
for(UIView *subSubView in [subView subviews]) {
if([subSubView conformsToProtocol:#protocol(UITextInputTraits)]) {
[(UITextField *)subSubView setReturnKeyType: UIReturnKeyDone];
}
}
}
}
One more useful hint, to the Run Loop code (in "#try") section.
This enabled "Done" button when text field is empty:
UITextField *tf = (UITextField *)searchBarSubview;
tf.enablesReturnKeyAutomatically = NO;
For Swift to change return key of UISearchBar
searchBar.returnKeyType = UIReturnKeyType.done
enum available are as below
public enum UIReturnKeyType : Int {
case default
case go
case google
case join
case next
case route
case search
case send
case yahoo
case done
case emergencyCall
#available(iOS 9.0, *)
case continue
}
Just a reminder! If you searchBar stays as first responder, after you change your returnKeyType, you need to dismiss your keyboard and pop it up again to see the changes.
search.resignFirstResponder()
searchBar.returnKeyType = UIReturnKeyType.Done
search.becomeFirstResponder()
As it is a protocol with optional methods, you should test each method separately instead of try-catching.
for (UIView *searchBarSubview in searchBar.subviews)
{
if ([searchBarSubview conformsToProtocol:#protocol(UITextInputTraits)])
{
// keyboard appearance
if ([searchBarSubview respondsToSelector:#selector(setKeyboardAppearance:)])
[(id<UITextInputTraits>)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];
// return key
if ([searchBarSubview respondsToSelector:#selector(setReturnKeyType:)])
[(id<UITextInputTraits>)searchBarSubview setReturnKeyType:UIReturnKeyDone];
// return key disabled when empty text
if ([searchBarSubview respondsToSelector:#selector(setEnablesReturnKeyAutomatically:)])
[(id<UITextInputTraits>)searchBarSubview setEnablesReturnKeyAutomatically:NO];
// breaking the loop when we are done
break;
}
}
This will work for iOS <= 6. For iOS >= 7, you need to loop in searchBar.subviews[0].subviews.
Since the Alert-style keyboards are semi-transparent, I can see my view behind it. It doesn't look very good since I have multiple elements behind the keyboard that makes it hard for the keys to stand out. I wanted an all-black keyboard.
So I animated a black UIImageView into position behind the keyboard when text is edited. This gives the appearance of an all-black keyboard.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.25];
blackBoxForKeyboard.frame = CGRectMake(0, 377, 320, 216);
[UIView commitAnimations];
}
I tried all the solutions shown here, and none of them worked for my UISearchBar (xcode5 compiling for iOS7). I ended up with this recursive function which worked for me:
- (void)fixSearchBarKeyboard:(UIView*)searchBarOrSubView {
if([searchBarOrSubView conformsToProtocol:#protocol(UITextInputTraits)]) {
if ([searchBarOrSubView respondsToSelector:#selector(setKeyboardAppearance:)])
[(id<UITextInputTraits>)searchBarOrSubView setKeyboardAppearance:UIKeyboardAppearanceAlert];
if ([searchBarOrSubView respondsToSelector:#selector(setReturnKeyType:)])
[(id<UITextInputTraits>)searchBarOrSubView setReturnKeyType:UIReturnKeyDone];
if ([searchBarOrSubView respondsToSelector:#selector(setEnablesReturnKeyAutomatically:)])
[(id<UITextInputTraits>)searchBarOrSubView setEnablesReturnKeyAutomatically:NO];
}
for(UIView *subView in [searchBarOrSubView subviews]) {
[self fixSearchBarKeyboard:subView];
}
}
I then called it like so:
_searchBar = [[UISearchBar alloc] init];
[self fixSearchBarKeyboard:_searchBar];
Just for covering all the iOS versions:
NSArray *subviews = [[[UIDevice currentDevice] systemVersion] floatValue] < 7 ? _searchBar.subviews : _searchBar.subviews[0].subviews;
for (UIView *subview in subviews)
{
if ([subview conformsToProtocol:#protocol(UITextInputTraits)])
{
UITextField *textField = (UITextField *)subview;
[textField setKeyboardAppearance: UIKeyboardAppearanceAlert];
textField.returnKeyType = UIReturnKeyDone;
break;
}
}