I have a ViewController with a subview that contains a UISearchBar. When I click on the search bar the keyboard appears and I am able to close it using the cancel button. The problem seems to be that it isn't accepting any of the input when a user clicks on the keyboard buttons.
#pragma mark -
#pragma mark SEARCH METHODS
#pragma mark -
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
self.mainSearchBar.showsCancelButton = YES;
return YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[self.mainSearchBar resignFirstResponder];
self.mainSearchBar.showsCancelButton = NO;
self.mainSearchBar.text = #"";
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self.mainSearchBar resignFirstResponder];
}
Has anyone had this issue before or know how to fix it?
** UPDATE *****
It turns out the Siri will work when trying to edit the UISearchBar. Still no luck with the keyboard input though. I think it has something to do with the fact that the UISearchBar is situated on a UIView that is then added as a subview. Not sure how to fix it though.
** UPDATE 2 *****
Moved the search box to the main UIView and I am still getting the same error. I am wondering if it is now related to the Navigation Controller.
Make sure that the app delegate has a [self.window makeKeyAndVisible]. Hopefully it will solve your problem.
Check whether you have added a UISearchBarDelegate in your viewController's .h file.
Try this code also:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSLog(#"%#",searchText);
}
This has to be related to how the SearchBar is rendered in relation to the ViewController's view. Why are you making the SearchBar a subview of another view rather than a subview of the ViewController's view?
Problem solved, the answer was found on a previous thread Keyboard and cursor show, but I can't type inside UITextFields and UITextViews
It turns out that in IOS6 the Visible at Launch check needs to be clicked on the options.
Related
When I updated my xcode and ios 11.0 ...Some UI changed. I mean there alignment and positions.
One serious issue is coming in my project is that, keyboard is not hiding.
Scenario is Suppose I am typing something in UITextfield and move to next UITextfield. Here in second UITextfield I used UIActionSheet pickerview. So, when i click on second UITextfield then UIActionSheet pickerview is coming as well as previous keyboard is not hiding. UIActionSheet picker is showing back of keyboard and keyboard is not hiding.
See the above Image.
I already posted this issue here,
UITextView issue with ActionSheetStringPicker Picker view
again same question..! I already given solution for this question. Check below link,
UITextView issue with ActionSheetStringPicker Picker view
Here my answer
#pragma mark - UITextField Delegate methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isEqual:txtFldActionSheetPicker]) {
[textField resignFirstResponder];
return NO;
}
else
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
I'm using MMDrawerController as a side menu. I can't find where to close the keyboard in center view when I open the side menu.
I've tried
write [self.view endEditing:YES] in sideMenuViewController's viewWillAppear method.
addObserver in centerViewController which called [self.view endEditing:YES] and postObserver in sideMenuViewController's viewWillAppear method.
It just don't work and cause like this...
Anyone has an idea to fix it?
today I met the same problem :)
I have spend several hours debugging the MMDrawer library and I have found solution of some kind. My solutions contains of two steps:
1) In MMDrawerController.m file - in the implementation of method: -(void)toggleDrawerSide:(MMDrawerSide)drawerSide animated:(BOOL)animated completion:(void (^)(BOOL finished))completion add at the beginning of the method, the following code: [self.centerViewController.view endEditing:true];. With this code you close the keyboard when tapping on menu burger - all views that are subviews for the current centerViewController try to resignFirstResponder.
2) In MMDrawerController.m file - in the implementation of method: -(void)panGestureCallback:(UIPanGestureRecognizer *)panGesture replace the else statement of case UIGestureRecognizerStateBegan with the following code:
else {
//hide keyboard when pan gesture start
[self.centerViewController.view endEditing:true];
self.startingPanRect = self.centerContainerView.frame;
}
And I am giving you the old code - coming from the library just for more detail explanation:
else {
self.startingPanRect = self.centerContainerView.frame;
}
With this code you close the keyboard when starting pan gesture, if such pan gesture is allowed for side menu - all views that are subviews for the current centerViewController try to resignFirstResponder.
P.S. I have tried to put this code in another method, but it behave a little strange because of the animation completion block after the movement of the centerViewController is done. So I think endEditing: in the beginning of the pan gesture is the way to go.
Hope that could help you!
I found the solution
In your CentralView controller
#pragma mark - Button Handlers
-(void)leftDrawerButtonPress:(id)sender{
[self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
[self.textfield resignFirstResponder];
}
Try this method. It is working for me
You don't have a use observer for this, just have IBOutlet property for your UITextField say "textfield".
[self.textfield resignFirstResponder];
add this line when you sideMenuViewController's viewWillAppear.
For anyone who still has problems with opening the drawer using a gesture, use the following code:
Put the code where you initialize your drawer controllers.
[self.drawerController setGestureCompletionBlock:^(MMDrawerController *drawerController, UIGestureRecognizer *gesture) {
// hide the keyboard when the gesture completes
if(drawerController.openSide == MMDrawerSideLeft) {
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
}];
With help from other posts on this site, I have managed to open up search display controller with keyboard when a button is tapped. I can see the underlying UIView is dimmed too.
What I now want to do is, not have the keyboard dismissed when the user taps outside the search bar. Currently, the keyboard gets dismissed and searchDisplayControllerWillEndSearch is invoked.
I have tried combinations of the following but none worked.
self.searchDisplayController.searchBar.hidden = NO;
[self.searchDisplayController.searchBar setShowsCancelButton:YES animated:YES];
[self.searchDisplayController setActive: YES animated: YES];
[self.searchDisplayController.searchBar becomeFirstResponder]
Can anyone please suggest any ideas?
I haven't been able to try this, but I think it should work
Declare something as searchbar delegate;
#interface YourViewController : UIViewController <UISearchBarDelegate>
Then, somewhere in YourViewController.m, set:
- (BOOL) searchBarShouldEndEditing:(UISearchBar *)searchBar {
return NO;
}
You can set it to return something dynamic, for instance if we only wanted to allow it to stop editing if there isn't any text, we could do this.
- (BOOL) searchBarShouldEndEditing:(UISearchBar *)searchBar {
return (searchbar.text.length > 0) ? YES : NO;
}
This way, if the user has started typing, or any text is in the search bar, it won't resign.
Let me know if this works.
I'm debugging my code, where UISearchBar's delegate method searchBarTextDidBeginEditing: is called exactly twice every time I tap on the search bar (which is in the navigation bar).
The odd thing is that only this delegate method is called twice. The others are called only once within the whole proccess, which is the correct behavior.
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
// called only once
return YES;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// called twice every time
[searchBar setShowsCancelButton:YES animated:YES];
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
// called only once
[searchBar setShowsCancelButton:NO animated:YES];
return YES;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
// called only once
}
Any idea what could be wrong?
The UISeachrBar is set up in Storyboard, with properly connected outlets, although it's not added to any view, and in the particular view controller's viewDidLoad is the following line that adds the search bar to the navigation bar:
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
I'm using Xcode 5.0.1 and running the code in iOS 7.0.3 Simulator.
I was experiencing the same issue, and dug in a bit deeper.
In my case, I had a subclass of UISearchDisplayController which functioned as UISearchDisplayDelegate for itself, and UISearchBarDelegate for its UISearchBar.
As it turns out, the problem is that UISearchDisplayController implements the following methods that collide with theUISearchBarDelegate` protocol:
- (void)searchBar:(id)arg1 textDidChange:(id)arg2;
- (void)searchBarCancelButtonClicked:(id)arg1;
- (void)searchBarResultsListButtonClicked:(id)arg1;
- (void)searchBarSearchButtonClicked:(id)arg1;
- (void)searchBarTextDidBeginEditing:(id)arg1;
This means if you make a UISearchDisplayController the delegate of its own UISearchBar, those methods will be called twice.
I found that if you unset the searchBar delegate and only keep the searchDisplayController delegate the method is not called at all any more. So the only workaround i could come up with is putting this in the beginning of your searchBarTextDidBeginEditing and searchBarTextDidEndEditing.
static NSDate *lastInvocation;
if ([[NSDate date] timeIntervalSinceDate:lastInvocation] < 0.1f) {
lastInvocation = [NSDate date];
return;
} else {
lastInvocation = [NSDate date];
}
I found this solution:
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
But funny thing is, I removed it after some two rounds of testing, the code is just called once, not twice
I had problem with searchBarSearchButtonClicked: method been called twice.
Problem was solved by calling [searchBar resignFirstResponder];
#pragma mark - UISearchViewDelegate methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
// Do things
[searchBar resignFirstResponder];
}
I have a Controller with a search bar at top, and when the user types, autocompleted search results are shown below it, in a UIScrollView.
Problem:
The results show up, but the user can't scroll those results. The scrollview is frozen. The only way to scroll is to push 'Cancel' in the Search bar. Tapping "Search" hides the keyboard, but even then the scrollview is frozen.
Desired:
As the user is typing, search results are being autocompleted. At any given time, the user can scroll through those results. They should not have to hit Cancel in order to scroll.
There are more results than will fit on the screen so this isn't an issue of content being only slightly larger than the screen.
Nothing special in CancelButtonClicked or SearchButtonClicked:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text = #"";
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
[NSThread detachNewThreadSelector:#selector(fetchSearchResult:) toTarget:self withObject:searchBar.text];
}
- (void)searchBar:(UISearchBar *)searchBar
textDidChange:(NSString *)searchText {
if([searchBar.text length] >=3){
[mySpinner startAnimating];
[NSThread detachNewThreadSelector:#selector(fetchSearchResult:) toTarget:self withObject:searchBar.text];
}
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
self.searchDisplayController.searchResultsTableView.hidden = YES;
// This occurs when user starts entering text
// We want to keep the background area dark
}
I had possibly the same issue: UISearchBar was nested into UIScrollView, so when search results was shown- It was impossible to scroll parent view.
..I was puzzled but I learned (UISearchDisplayCtrl private api) that UISearchDisplayController locks all parent scroll views (bitch).
So U should add category UISearchDisplayController+Custom:
#implementation UISearchDisplayController (Custom)
- (void)_disableParentScrollViews {
}
- (void)_enableParentScrollViews {
}
#end