I am facing an issue with the UISearchController in iOS 8. The problem is that whenever I am clicking on cancel button, instead of cancelling the search operation, it is bringing up the keyboard.
Follow below steps to reproduce the issue -
Click on the search bar.
Type a word and click on search(This will
dismiss the keyboard)
Now click on cancel.
Now, instead of dismissing the search, the keyboard is popping up for entering the text. This usually happens when I am tapping at the right half of cancel button and it can be reproduced even in standard iOS apps like Contacts app. I would like to dismiss search when cancel is tapped.
Is there any workaround for this issue?
You could remove the search bar as a first responder programatically when the cancel button is clicked which will prevent the keyboard from coming up.
[searchController.textField resignFirstResponder];
Related
I have searchBar and tableView. I have a bug, when user clicks on "hide keyboard". I have tried to user Notification Center for hiding keyboard. But it also works for "cancel" button. Maybe there is a way to exclude determining "cancel button"?
On hide keyboard button click listener you can use two from the below options
1) self.view.endEditing(true)
This will notify your current active view to stop editing which will
automatically hides the keyboard
2) yourTextField.resignFirstResponder
This will stop focusing your current editing textView which will
automatically hide the keyboard.
As you will write this code specific to your Hide keyboard button it won't apply the same solution to your Cancel button.
I have encountered a problem that is my search bar doesn't hide when I tap back navigation button.
In fact, isMovingFromViewController doesn't respond when search bar is active.
I would like to know when back button is tapped so that I could make the search bar inactive and hide the keyboard.
Have you ever seen this error before?
If so, please let me know how to fix it.
Thank you
Some code would have been helpful, anyway to hide a view after a click add add a function to the button then call viewName.isHidden. but i cant suggest more until i see a code or at least a screen
I have a navigation bar with added searchbar icon.
Tapping the searchbar button instantiates the UISearchBarController.
Everything works fine, except that the 'Cancel' button tap propagates to the navigation bar button below (which closes the current modal).
Tapping cancel here:
Also taps done here:
I've witnessed that there is a delegate method available:
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
}
How do I stop the event from propagating?
Note: The propagation doesn't happen when tapping 'Cancel' after text was entered in the searchbar, only when it is empty. Very strange.
I think what you are seeing is that tapping Cancel when text is present is actually clearing out the text within the UITextField that the UISearchBar owns (as part of the UISearchController). Tapping Cancel otherwise is supposed to dismiss the search bar. From the Apple docs on searchBarCancelButtonClicked: it states that:
Typically, you implement this method to dismiss the search bar.
I've ran into this in the past with "search bar" functionality on a UITableViewController, and it's one of the reasons why I've switched over to using a standard UIViewController which owns a UITableView and owns a UISearchBar. I have a little bit more control, this way, over the content of my view controller, and the search bar I'm using with it.
Not sure if that directly helps you, but using a UISearchBar instead of a UISearchController might point you in a better direction like it did for me.
Rebooting the emulator fixed the problem, whatever that makes sense.
On the iPhone in Landscape orientation, there is a button in the lower right corner that causes the keyboard to dismiss. I really don't want this button to be there, since I have a Done button already and users should only be able to stop editing the textfield by pressing this Done button (which has to be there to address portrait orientation anyway).
I'm wondering if there's a way to remove that keyboard dismissal button, or if not, I'd appreciate any suggestions for UITextField delegate behavior that differentiates between proper dismissal (Done button pressed) and improper (first responder might resign even though Done button not pressed, e.g. because of aforementioned problem button or view controller dismissal).
One thing you can do is declare a variable, something like, isDismissedProperly and initiate it to false. Then subscribe to keyboard show and hide notifications. UIKeyboardDidShowNotification and UIKeyboardDidHideNotification. In the target method for done button(as mentioned in the question), change the isDismissedProperly to true. And in the keyboardDidHide method, you can check if isDismissedProperly is true or false.
If it's true, then user dismissed the keyboard by pressing done button.
Hope it helps!
while I develop iOS application with swift, I wondered why apple did not support auto-dismiss keyboard function in application. This means if I implement a TextLabel on screen, I tapped that label, Keyboard appears, but did not dismissed automatically.
I thought many of application keyboards need to dismissed when users tap on outside of the keyboard screen or click 'done' button on a screen. However, basically, I have to implement keyboard dismiss function on every ViewController. And I think this is code duplication.
Anyone can explain me about apple's application method implementation philosophy and how can I wrote an reusable function, which is not duplicated function in every ViewController scheme.
Transposed from Comments:
In general apps where the main function utilizes the keyboard would rather control when the keyboard dismisses themselves instead on relying on an auto feature.
Ex: For a texting app after I click the send button I would like to send another text. Then I shouldn't dismiss the keyboard.
If you are dismissing the keyboard multiple times than I would try making a view controller class that handles that and subclass it. You can observe when the keyboard is shown and add a button on top of your view so when it is clicked the keyboard dismisses using [self.view endEditing:YES];