iOS8 Cannot hide cancel button on search bar in UISearchController

后端 未结 9 552
后悔当初
后悔当初 2021-01-01 12:26

My goal is to prevent the cancel button from appearing in a search bar in a UISearchController. I started with Apple\'s Table Search with UISearchController sample code and

相关标签:
9条回答
  • 2021-01-01 12:50

    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.

    FJSearchBar

    Marking searchController.searchBar.showsCancelButton = NO doesn't seem to work in iOS 8. I haven't tested iOS 9.

    FJSearchBar.h

    Empty, but placed here for completeness.

    @import UIKit;
    
    @interface FJSearchBar : UISearchBar
    
    @end
    

    FJSearchBar.m

    #import "FJSearchBar.h"
    
    @implementation FJSearchBar
    
    - (void)setShowsCancelButton:(BOOL)showsCancelButton {
        // do nothing
    }
    
    - (void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated {
        // do nothing
    }
    
    @end
    

    FJSearchController

    Here's where you want to make the real changes. I split the UISearchBarDelegate into its own category because, IMHO, the categories make the classes cleaner and easier to maintain. If you want to keep the delegate within the main class interface/implementation, you're more than welcome to do so.

    FJSearchController.h

    @import UIKit;
    
    @interface FJSearchController : UISearchController
    
    @end
    
    @interface FJSearchController (UISearchBarDelegate) <UISearchBarDelegate>
    
    @end
    

    FJSearchController.m

    #import "FJSearchController.h"
    #import "FJSearchBar.h"
    
    @implementation FJSearchController {
    @private
        FJSearchBar *_searchBar;
        BOOL _clearedOutside;
    }
    
    - (UISearchBar *)searchBar {
        if (_searchBar == nil) {
            // if you're not hiding the cancel button, simply uncomment the line below and delete the FJSearchBar alloc/init
            // _searchBar = [[UISearchBar alloc] init];
            _searchBar = [[FJSearchBar alloc] init];
            _searchBar.delegate = self;
        }
        return _searchBar;
    }
    
    @end
    
    @implementation FJSearchController (UISearchBarDelegate)
    
    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
        // if we cleared from outside then we should not allow any new editing
        BOOL shouldAllowEditing = !_clearedOutside;
        _clearedOutside = NO;
        return shouldAllowEditing;
    }
    
    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
        // hide the keyboard since the user will no longer add any more input
        [searchBar resignFirstResponder];
    }
    
    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
        if (![searchBar isFirstResponder]) {
            // the user cleared the search while not in typing mode, so we should deactivate searching
            self.active = NO;
            _clearedOutside = YES;
            return;
        }
        // update the search results
        [self.searchResultsUpdater updateSearchResultsForSearchController:self];
    }
    
    @end
    

    Some parts to note:

    1. I've put the search bar and the BOOL as private variables instead of properties because
      • They're more lightweight than private properties.
      • They don't need to be seen or modified by the outside world.
    2. We check whether the searchBar is the first responder. If it's not, then we actually deactivate the search controller because the text is empty and we're no longer searching. If you really want to be sure, you can also ensure that searchText.length == 0.
    3. searchBar:textDidChange: is invoked before searchBarShouldBeginEditing:, which is why we handled it in this order.
    4. I update the search results every time the text changes, but you may want to move the [self.searchResultsUpdater updateSearchResultsForSearchController:self]; to searchBarSearchButtonClicked: if you only want the search performed after the user presses the Search button.
    0 讨论(0)
  • 2021-01-01 12:54

    This was the simplest solution I could come up with in Swift.

    Custom search controller:

    class CustomSearchController: UISearchController {
    
        var _searchBar: CustomSearchBar
    
        override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
            self._searchBar = CustomSearchBar()
            super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        }
    
        override init(searchResultsController: UIViewController?) {
            self._searchBar = CustomSearchBar()
            super.init(searchResultsController: searchResultsController)
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        override var searchBar: UISearchBar {
            return self._searchBar
        }
    }
    

    Custom search bar:

    class CustomSearchBar: UISearchBar {
        override func setShowsCancelButton(showsCancelButton: Bool, animated: Bool) {
            // do nothing
        }
    }
    

    The most important piece of this was to only create the _searchBar object once in init vs. creating it inside of the stored property.

    0 讨论(0)
  • 2021-01-01 12:55

    Just subclass your UISearchController and do the following:

    class CustomSearchController: UISearchController {
    
       override func viewDidLayoutSubviews() {
           super.viewDidLayoutSubviews()
           searchBar.showsCancelButton = false
       }
    }
    

    This was the easiest solution I could came up with in order to solve the flashing cancel-button issue.

    0 讨论(0)
提交回复
热议问题