How to enable cancel button with UISearchBar?

uisearchbar cancel button not showing
uisearchbar show cancel button when editing
hide cancel button uisearchbar swift
search bar close button clicked swift
automatically show cancel button
uisearchcontroller vs uisearchbar
uisearchbar cancel button color
uisearchbar hide cancel button

In the contacts app on the iPhone if you enter a search term, then tap the "Search" button, the keyboard is hidden, BUT the cancel button is still enabled. In my app the cancel button gets disabled when I call resignFirstResponder.

Anyone know how to hide the keyboard while maintaining the cancel button in an enabled state?

I use the following code:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

The keyboard slides out of view, but the "Cancel" button to the right of the search text field is disabled, so that I cannot cancel the search. The contacts app maintains the cancel button in an enabled state.

I think maybe one solution is to dive into the searchBar object and call resignFirstResponder on the actual text field, rather than the search bar itself.

Any input appreciated.

try this

for(id subview in [yourSearchBar subviews])
{
    if ([subview isKindOfClass:[UIButton class]]) {
        [subview setEnabled:YES];
    }
}

Cancel Button Becomes Inactive - Open Q&A, YES to use animation to change the display state of the cancel button, otherwise NO . Discussion. Cancel buttons are not displayed for apps running on iPad, even� How to enable cancel button with UISearchBar? (12) In the contacts app on the iPhone if you enter a search term, then tap the "Search" button, the keyboard is hidden, BUT the cancel button is still enabled. In my app the cancel button gets disabled when I call resignFirstResponder.

This method worked in iOS7.

- (void)enableCancelButton:(UISearchBar *)searchBar
{
    for (UIView *view in searchBar.subviews)
    {
        for (id subview in view.subviews)
        {
            if ( [subview isKindOfClass:[UIButton class]] )
            {
                [subview setEnabled:YES];
                NSLog(@"enableCancelButton");
                return;
            }
        }
    }
}

(Also be sure to call it anywhere after [_searchBar resignFirstResponder] is used.)

setShowsCancelButton:animated:, In the contacts app on the iPhone if you enter a search term, then tap the "Search " button, the keyboard is hidden, BUT the cancel button is still enabled. Hi all. First of all, thanks for your hard work on KIF. This library has been a godsend. :P I'm trying to programmatically tap the "Cancel" button of a UISearchBar. I've tried cycling through its subviews to no avail, and hardcoding a po

The accepted solution will not work when you start scrolling the table instead of tapping the "Search" button. In that case the "Cancel" button will be disabled.

This is my solution that re-enables the "Cancel" button every time it is disabled by using KVO.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // Search for Cancel button in searchbar, enable it and add key-value observer.
    for (id subview in [self.searchBar subviews]) {
        if ([subview isKindOfClass:[UIButton class]]) {
            [subview setEnabled:YES];
            [subview addObserver:self forKeyPath:@"enabled" options:NSKeyValueObservingOptionNew context:nil];
        }
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    // Remove observer for the Cancel button in searchBar.
    for (id subview in [self.searchBar subviews]) {
        if ([subview isKindOfClass:[UIButton class]])
            [subview removeObserver:self forKeyPath:@"enabled"];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    // Re-enable the Cancel button in searchBar.
    if ([object isKindOfClass:[UIButton class]] && [keyPath isEqualToString:@"enabled"]) {
        UIButton *button = object;
        if (!button.enabled)
            button.enabled = YES;
    }
}

How to enable cancel button with UISearchBar?, I have my UISearchBar placed in a navigation bar. When I tap on the bar, it expands itself and shows the cancel button. If you type something in the field and � Subclassing UISearchBar and overriding setShowsCancelButton: and setShowsCancelButton:animated: hides the cancel button. SOLUTION. I set active to NO if the search bar is not the first responder (keyboard is not active and displayed), since that is effectively a cancel command. FJSearchBar

As of iOS 6, the button appears to be a UINavigationButton (private class) instead of a UIButton.

I have tweaked the above example to look like this.

for (UIView *v in searchBar.subviews) {
    if ([v isKindOfClass:[UIControl class]]) {
        ((UIControl *)v).enabled = YES;
    }
}

However, this is obviously brittle, since we're mucking around with the internals. It also can enable more than the button, but it works for me until a better solution is found.

We should ask Apple to expose this.

UISearchBar cancel button disabled when keyboard disappears on , The cancel button in UISearchBar is enabled even when the keyboard is dismissed. My app behaves differently (right screenshot). The cancel button� How to change the default text of Cancel Button which appears in the UISearchBar+iPhone (10) HI I am developing an Application where i wanted to change the text of Search String in the SearchBar. I wanted to change the text of Cancel Button Also which appears next to the SearchBar.

This seemed to work for me (in viewDidLoad):

__unused UISearchDisplayController* searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];

I realize I should probably be using the UISearchDisplayController properly, but this was an easy fix for my current implementation.

How do you keep the cancel button in the search bar enabled when , I found a solution. You can use this for-loop to loop over the subviews of the search bar and enable it when the search button is pressed on the keyboard. for � Overview. UISearch Bar provides a text field for entering text, a search button, a bookmark button, and a cancel button. A search bar does not actually perform any searches. You use a delegate, an object conforming to the UISearch Bar Delegate protocol, to implement the actions when text is entered and buttons are click

UISearchBar disable auto disable of cancel button, It will recursively traverse all subviews of the search bar to make sure it enables all UIControl s (which includes the Cancel button). TL;DR: Subclassing UISearchBar and overriding setShowsCancelButton: and setShowsCancelButton:animated: hides the cancel button.. SOLUTION. I set active to NO if the search bar is not the first responder (keyboard is not active and displayed), since that is effectively a cancel command.

How to enable cancel button with UISearchBar?, The docs are not clear on this one, as only since 7.1, the UISearchBar implements the UITextInputTraits protocol, but it is not noted since which iOS version the� With UISearchBar and the UISearchController, iOS provides much of the functionality out of the box, so there’s no excuse not to use it. The ability to search a large table view is something that today’s users expect; without it, they won’t be happy campers.

iphone UISearchBar Done button always enabled, In the contacts app on the iPhone if you enter a search term, then tap the "Search " button, the keyboard is hidden, BUT the cancel button is still� The UISearchBar is used to search through a list of values. It contains three main components: A field used to enter text. Users can utilize this to enter their search term. A clear button, to remove any text from the search field. A Cancel button, to exit the search function. Implementing the Search Bar

Comments
  • If someone is looking for a C# solution one can find it here.
  • See my note to Ben below. I decided to remove the code to access the internals in order to avoid any issues. Tapping the circle "x" in the search field re-enables the cancel button, and so hopefully it will not be an issue for users.
  • This solution will not work when you start scrolling the table instead of tapping the "Search" button. In that case the "Cancel" button will be disabled. See my old answer bellow that deals with this problem.
  • This won't work in iOS 10. UISearchBar view hierarchy was changed. You need to go one level deeper.
  • +1 for "Also be sure to call it anywhere after [_searchBar resignFirstResponder] is used."
  • This answer worked, but the answer marked as correct in this thread didn't work in iOS 10.
  • Nice solution. Note that you can capture scroll events by implementing - (void)scrollViewDidScroll:(UIScrollView *)scrollView and re-enable the button there as well.
  • Thanks Ben! I ran some tests with my existing code and found that if I tap the circle "x" within the search field, the cancel button is re-enabled once again. This is on iOS 5.1. I have not yet converted to iOS 6. Therefore, I decided to remove the code to re-enable the cancel button as I want to avoid issues with accessing internals. As you say, it would be nice if the API allowed us direct access to this button. I plan to play safe for now :-) Thank you for posting.
  • I did a mixture of this approach with the additional delay – otherwise the setEnabled would not be recognized or overwritten internally... see stackoverflow.com/questions/8536625/uisearchbar-cancel-button
  • The cleanest solution for me
  • 'UISearchDisplayController' is deprecated: first deprecated in iOS 8.0 - UISearchDisplayController has been replaced with UISearchController :-(
  • Isn't this runtime access a private API and possibility of app rejection?
  • Nope this is not a private API but Key-Value coding. App won't be rejected.
  • Sounds this should work and I find it "cleaner" solution then my solution with KVO. +1