Return on no text in UISearchbar - ios

I have a UISearchbar that has the return key on the keyboard that is greyed out unless I type text into the searchbar. How do I allow it to appear on no text? If thats not possible, how do I return to my view if theres no text in the searchbar?
Heres some code:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
touchtoCancel = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-211)];
[touchtoCancel addTarget:self
action:#selector(DidPressCancelSearch:)
forControlEvents:UIControlEventTouchUpInside];
[self.search setFrame:CGRectMake(self.search.frame.origin.x, self.search.frame.origin.y-210,
self.search.frame.size.width, self.search.frame.size.height)];
return YES;
}
-(IBAction)DidPressCancelSearch:(id)sender
{
[search resignFirstResponder];
}

One method besides the built-in cancel button that was already mentioned... You can create an invisible button that takes up the area above the keyboard which enables only when editing the search bar. Tapping on this invisible button (space between search bar and top of the keyboard) should resign the first responder.

Related

I am getting error like "Assignment to read only property" in UIView

If VIN is an text field this code works for me to Hiding keyboard without breaking UITextField functionality..
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
VIN.inputView = dummyView;
But in another case i need to do same thing for UIVIEW.. it is possible to do for UIView ?
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
VIN.inputView = dummyView;
In this case VIN Is an UIView.. i am getting error like "Assignment to read only property".
UITextfield has inputview. Default inputview is keyboard. uiview has not input view that you can set so you can't assign input view to UIView.
And for hiding keyboard you should call resignFirstresponder like,
[myTextField resignFirstResponder];
This will hide keyboard and when you click textfield it will show keyboard again so you should do like this.
Hope this will help :)
If you want to hide or disable keyboard for particular textfield , you can use following textfield delegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// Here You can do additional code or task instead of writing with keyboard
if (textField == YourTextField)
{
return NO;//Keyboar Wont appear for textfield
}
return YES;
}

How to programmatically deselect UITextField

I have a UITextField which I can click on, and edit the contents of. I am running code when the UITextField is pressed by using:
[personalCountryLabel addTarget:self action:#selector(countryPressed) forControlEvents:UIControlEventEditingDidBegin];
This presents another view controller. However, when I click the back button, the UITextField is still selected, so the text runs again, sending me back to the view controller.
I use the code:
textField.enabled = false;
and
textField.enabled = true;
to respectively turn off and on the editing of the UITextField, but doing this in succession does not unselect the UITextField.
How can I therefore programmatically deselect the UITextField (i.e, where the line cursor is no longer blinking).
If I understand what you're asking correctly, you just want:
[textField resignFirstResponder];
/* Programmatically deselect the Uitextfiels below this Code */
UITextField *txtDeselect = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.origin.x+20, self.view.frame.origin.y+20, self.view.frame.size.width-40, 40)];
[txtDeselect setBackgroundColor:[UIColor yellowColor]];
[txtDeselect setBorderStyle:UITextBorderStyleRoundedRect];
[txtDeselect setTextAlignment:NSTextAlignmentCenter];
[txtDeselect setText:#"Email"];
txtDeselect.enabled = NO;
[self.view addSubview:txtDeselect];
did you set outlet & delegate of UITextField in your view controller ?
why you use UItextfield for this ?
i recommend you use one of this before presenting new view controller :
option 1 :
[youtextfield resignFirstResponder];
//please sure you outlet connected & ....
you can call this on your viewWillAppear
option 2 :
[self.view endEditing:YES]; // this regularly
happend after present another VC)
you shouldn't use shouldEndEditing

Why does UIKeyboard appear when press cancel button on UISearchbar?

I am using following code for adding UISearchBar in iOS7 and UISearchBar appearance is fine.But problem is when press cancel button on UISearchBar then UIKeyboard appeared.Why its happen?Please help me.
CGRect searchFrame = CGRectMake(self.view.bounds.origin.x, 64, self.view.bounds.size.width, 44.0f);
self.mySearchBar = [[UISearchBar alloc]initWithFrame:searchFrame];
self.mySearchBar.delegate = self;
self.mySearchBar.searchBarStyle = UISearchBarStyleDefault;
self.mySearchBar.placeholder = #"search items;
self.mySearchBar.showsCancelButton = YES;
[self.view addSubview:self.mySearchBar];
Implement the searchBarCancelButtonClicked: UISearchBarDelegate method. If you want the keyboard to go away you would do something like:
- (void) searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
I got your issue now.
When there is not text entered at that time cancel button will be disabled. So if you try to press it than it will consider SearchBar text area & Keyboard will appear.
While you enter the text than Cancel button will be active & than it will work.
Hope you'll get the solution from this.

Dismiss Keyboard

So I have a self created top bar controller that is being implemented in my other controllers views. I have a textfield on this top bar. I was wondering what the best approach to having the keyboard dismiss if the user clicks anywere outside the keyboard. I do have a tap gesture recognizer that performs the method dismisskeyboard. However, this only works if the user clicks on the top bar outside the keyboard. Is there a way to set it up so if the user clicks anywere on the screen, then this will dismiss the keyboard?
The approach I would describe is a hack but still works.
create a transparent UIButton with the frame of the view, like below:
UIButton* overlay = [UIButton buttonWithType:UIButtonTypeCustom];
overlay.frame = self.view.bounds;
overlay.backgroundColor = [UIColor clearColor];
[overlay addTarget:self action:#selector(hideOverlay:) forControlEvents:UIControlEventTouchDown];
[self.view.subviews[0] insertSubview:overlay belowSubview:self.textField];
Create a method hideOverlay to dismiss the keyboard and hide the transparent:
-(void)hideOverlay:(id)sender {
UIView* overlay = sender;
[overlay removeFromSuperview];
[self.textField resignFirstResponder];
}
You should ideally call the first block of code in textFieldDidBeginEditing: protocol method of UITextFieldDelegate and you should register your calling class accordingly.
You might try giving the text field a transparent inputAccessoryView, sized to fill the rest of the screen, that catches taps and dismisses the keyboard.

SearchBar in UINavigationItem will not be first controller

I have a UISearchBar in my UINavigationItem like so:
searchBar = [[UISearchBar alloc] init];
[searchBar setDelegate:self];
self.navigationItem.titleView = searchBar;
Then in my table view I use:
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.searchBar resignFirstResponder];
}
--so that the keyboard will disappear if the user scrolls through the results.
However, now when I click on the search bar again, I can't get it to pull up the keyboard. I figured the view would do it automatically but it doesn't (clearly). I tried:
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar becomeFirstResponder];
}
It didn't work (and I didn't expect it to)
searchBarTextDidBeginEditing is not going to be called until after the search bar becomes first responder, so clearly your becomeFirstResponder call is not even being called.
I am just as surprised as you are that the search bar does not just automatically become first responder when it is tapped. Is some other code (not shown above) disabling it? Did you implement searchBarShouldBeginEditing: to return NO?
EDIT: After chat, turned out the problem was that he was not assigning the UISearchBar a frame (as you can see from his code).

Resources