UISearchBar's set_cancelButtonText: ivar is prohibited
Instead of messing around with the undocumented view hierarchy, you can use UIAppearance
:
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Whatever"
UISearchbar clearButton forces the keyboard to appear
This is an old question and I just came across the same issue and managed to solve it the following way:
When the searchBar:textDidChange:
method of the UISearchBarDelegate gets called because of the user tapping the 'clear' button, the searchBar hasn't become the first responder yet, so we can take advantage of that in order to detect when the user in fact intended to clear the search and not bring focus to the searchBar and/or do something else.
To keep track of that, we need to declare a BOOL
ivar in our viewController that is also the searchBar delegate (let's call it shouldBeginEditing
) and set it with an initial value of YES
(supposing our viewController class is called SearchViewController):
@interface SearchViewController : UIViewController <UISearchBarDelegate> {
// all of our ivar declarations go here...
BOOL shouldBeginEditing;
....
}
...
@end
@implementation SearchViewController
...
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
...
shouldBeginEditing = YES;
}
}
...
@end
Later on, in the UISearchBarDelegate, we implement the searchBar:textDidChange:
and searchBarShouldBeginEditing:
methods:
- (void)searchBar:(UISearchBar *)bar textDidChange:(NSString *)searchText {
NSLog(@"searchBar:textDidChange: isFirstResponder: %i", [self.searchBar isFirstResponder]);
if(![searchBar isFirstResponder]) {
// user tapped the 'clear' button
shouldBeginEditing = NO;
// do whatever I want to happen when the user clears the search...
}
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)bar {
// reset the shouldBeginEditing BOOL ivar to YES, but first take its value and use it to return it from the method call
BOOL boolToReturn = shouldBeginEditing;
shouldBeginEditing = YES;
return boolToReturn;
}
Basically, that's it.
Best
ios 13 - Custom SearchBar with UISearchBar _searchField not working
The SDK now provides UISearchBar.searchTextField
so you can simply replace your private API implementation with the public API.
searchBar.searchTextField.backgroundColor = [UIColor blueColor];
How to customize the searchBar in a UISearchController?
set attributedPlaceholder
for textfield of search bar
@IBOutlet weak var sbSearchBar: UISearchBar!
if let textfield = sbSearchBar.value(forKey: "searchField") as? UITextField {
textfield.backgroundColor = UIColor.red
textfield.attributedPlaceholder = NSAttributedString(string: textfield.placeholder ?? "", attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])
if let leftView = textfield.leftView as? UIImageView {
leftView.image = leftView.image?.withRenderingMode(.alwaysTemplate)
leftView.tintColor = UIColor.white
}
}
Here is result:
Update:
I think, this may help you: how to change uitextfield color in searchcontroller?
Just apply your color combination in this code and see.
if #available(iOS 11.0, *) {
let sc = UISearchController(searchResultsController: nil)
sc.delegate = self
let scb = sc.searchBar
scb.tintColor = UIColor.white
scb.barTintColor = UIColor.white
if let textfield = scb.value(forKey: "searchField") as? UITextField {
//textfield.textColor = // Set text color
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
// Rounded corner
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor.blue
}
navigationItem.searchController = sc
navigationItem.hidesSearchBarWhenScrolling = false
}
Result:
UISearchBar in iOS 13 have small black spot next to the dismiss (x) button
Per comments, https://stackoverflow.com/a/58040862/95309 have a fix:
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title
The difference between the UIButton.appearance
and UIBarButtonItem.appearance
seems to been the culprit.
How to change inside background color of UISearchBar component on iOS
Use this code to change the searchBar's UITextField
backgroundImage:
UITextField *searchField;
NSUInteger numViews = [searchBar.subviews count];
for (int i = 0; i < numViews; i++) {
if ([[searchBar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
searchField = [searchBar.subviews objectAtIndex:i];
}
}
if (searchField) {
searchField.textColor = [UIColor whiteColor];
[searchField setBackground: [UIImage imageNamed:@"yourImage"]]; //set your gray background image here
[searchField setBorderStyle:UITextBorderStyleNone];
}
Use the below code to change the UISearchBarIcon
:
UIImageView *searchIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourSearchBarIconImage"]];
searchIcon.frame = CGRectMake(10, 10, 24, 24);
[searchBar addSubview:searchIcon];
[searchIcon release];
Also, to change the searchBar icon you can use the following built-in method on UISearchBar
(which is available from iOS 5+):
- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state
Here you can set 4 types of UISearchBarIcon
i.e.:
UISearchBarIconBookmark
UISearchBarIconClear
UISearchBarIconResultsList
UISearchBarIconSearch
I hope this help you...
UISearchBar change placeholder color
for iOS5+
use the appearance proxy
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
Related Topics
Swift Alternative to Respondstoselector:
How to Access Cfdictionary in Swift 3
What Is the Slice Compare Logic in Swift
Using @Fetchrequest(Entity: ) for Swiftui MACos App Crashes
How to Retrieve All Contacts Using Cncontact.Predicateforcontacts
Nested Tabview - Remove Inner Tab Bar iOS 13, Swift Ui
Swift Pattern Matching with Enum and Optional Tuple Associated Values
Alamofire with Custom Parameter Encoding for Swift Application
Uicollectionview Scrolltoitematindexpath, Not Loading Visible Cells Until Animation Complete
How to Rotate an Arkit 4X4 Matrix Around Y Using Apple's Simd Library
Use Different Googleservice-Info.Plist for Single Project in Xcode Using Swift4
Contextmenu on a Rounded Lineargradient Produces Sharp Edges in Swiftui
String Comparison in Swift Is Not Transitive
Closures Return Value (Previously Completionblock)
How to Check If Cmtime Is Valid in Swift
Prevent Error "Funk" Sound in Event Monitor Os X