UISearchController disable cancel UIBarButtonItem
Updated in light of comments
UISearchBar
has a property (see the Apple docs) which determines whether the cancel button is displayed:
self.searchBar.showsCancelButton = false;
But, as per OP comments, this does not work, because the searchController keeps switching the cancel button back on. To avoid this, create a subclass of UISearchBar, and override the setShowsCancelButton
methods:
@implementation MySearchBar
-(void)setShowsCancelButton:(BOOL)showsCancelButton {
// Do nothing...
}
-(void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated {
// Do nothing....
}
@end
To ensure this subclass is used by the searchController, we also need to subclass UISearchController
, and override the searchBar
method to return an instance of our subclass. We also need to ensure that the new searchBar activates the searchController - I've chosen to use the UISearchBarDelegate
method textDidChange
for this:
@interface MySearchController () <UISearchBarDelegate> {
UISearchBar *_searchBar;
}
@end
@implementation MySearchController
-(UISearchBar *)searchBar {
if (_searchBar == nil) {
_searchBar = [[MySearchBar alloc] initWithFrame:CGRectZero];
_searchBar.delegate = self;
}
return _searchBar;
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if ([searchBar.text length] > 0) {
self.active = true;
} else {
self.active = false;
}
}
@end
Finally, change your code to instantiate this subclass:
self.searchController = [[MySearchController alloc] initWithSearchResultsController:self.resultsViewController];
(You will obviously need to import the relevant header files for these subclasses).
How to disable cancel button of UISearchController.seachBar when i tap at searchBar?
You can create a custom class and subclass UISearchBar and UISearchViewController.
For example:-
class CustomizedSearchBar: UISearchBar {
override func layoutSubviews() {
super.layoutSubviews()
setShowsCancelButton(false, animated: false)
}
}
Now Create Object of the CustomizedSearchBar and use it within other viewController.
Or you can create a customized searchViewController as follows:
class CustomizedSearchController: UISearchController, UISearchBarDelegate {
lazy var _searchBar: CustomSearchBar = {
[unowned self] in
let result = CustomSearchBar(frame: CGRectZero)
result.delegate = self
return result
}()
override var searchBar: UISearchBar {
get {
return _searchBar
}
}
}
Please follow this link for more detail information.
Hiding Cancel button on search bar in UISearchController
I ended up subclassing both UISearchBar
and UISearchController
as suggested:
CustomSearchBar.swift
import UIKit
class CustomSearchBar: UISearchBar {
override func layoutSubviews() {
super.layoutSubviews()
setShowsCancelButton(false, animated: false)
}
}
CustomSearchController.swift
import UIKit
class CustomSearchController: UISearchController, UISearchBarDelegate {
lazy var _searchBar: CustomSearchBar = {
[unowned self] in
let result = CustomSearchBar(frame: CGRectZero)
result.delegate = self
return result
}()
override var searchBar: UISearchBar {
get {
return _searchBar
}
}
}
Cancel Button in UISearchController
You need to set the UISearchController
searchBar's
delegate
. Once you have done this, the addition of the delegate method searchBarCancelButtonClicked:
will properly be called.
self.searchController.searchBar.delegate = self;
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
}
Related Topics
How to Get Touchid Information and Compare to a Fingerprint Database
Get Each Line of Text in a Uilabel
Create Custom International Keyboard for Iphone
Constraints for Centering One Button When the Other Is Hidden
Audiokit iOS Aksamplermetronome
How to Crop a Uiimage in Swift
Ios/C: Convert "Integer" into Four Character String
Uitableview Pagination - Bottom Refresh to Load New Data in Swift
How to Segue with Data from One Tab to Another Tab Properly
Cannot Invoke Initializer for Type 'Double' with an Argument List of Type '(String)'
Uisearchcontroller Disable Cancel Uibarbuttonitem
Fontawesome Pro and Xamarin.iOS Only One Font Can Be Active
Can the -Objc Flag Be Applied Selectively to Static Libraries
Avaudiosession .Defaulttospeaker Changes Mic Input