How to change Searchbar's Placeholder font and size in iOS13
Swift 5:
if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
let atrString = NSAttributedString(string: "Search",
attributes: [.foregroundColor : color,
.font : UIFont.systemFont(ofSize: 10, weight: .bold)])
textfield.attributedPlaceholder = atrString
}
How to change text color of PlaceHolder in UISearchBar? (iOS 13)
As you already mentioned, your code works only in viewDidAppear
, which makes the placeholder to flicker from the default gray color to the preferred color.
However, there seem to be a time before viewDidAppear
(I couldn't figure it out when exactly), to change the placeholder before the view actually appears.
I suppose, this may be connected to how iOS handles light/dark mode and/or an iOS bug.
The solution I came out with that works around the issue:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if #available(iOS 13.0, *) {
let placeholder = NSAttributedString(string: "Search",
attributes: [
.foregroundColor: UIColor.white
])
let searchTextField = searchBar.searchTextField
// Key workaround to be able to set attributedPlaceholder
DispatchQueue.global().async {
DispatchQueue.main.async {
searchTextField.attributedPlaceholder = placeholder
}
}
}
}
There seem to be no performance and/or other downside to this method.
If anybody else comes with a better approach, feel free to collaborate.
UISearchBar change placeholder color
for iOS5+
use the appearance proxy
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
How can I change the UISearchBar search text color?
You have to access the UITextField
inside the UISearchBar. You can do that by using valueForKey("searchField")
var textFieldInsideSearchBar = yourSearchbar.valueForKey("searchField") as? UITextField
textFieldInsideSearchBar?.textColor = yourcolor
Swift 3 update
let textFieldInsideSearchBar = yourSearchbar.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.textColor = yourcolor
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...
Related Topics
iOS 11 Search Bar Jumping to Top of Screen
App Running on iPad While Its Iphone-Only
Uploading Image with iOS App to Server File Size Is Too Large
How to Detect When User Used Password Autofill on a Uitextfield
Map Url Parameters to Objects Using Restkit
Add Custom Cordova Plugin to Ibm Worklight 6.1
How to Get Nsdate with Millisecond Accuracy
How to Retrieve a File/Resource in a iOS Framework Bundle from a Main Program
How to Change the Height of Uitextfield in Uialertcontroller in Swift
Autolayout: Origin and Size Should Change According to Width and Height Factor
iPhone Mkmapview - Mkpolygon Issues
Changing Iphone's Date and Time
Xcode /Podfile.Lock: No Such File
Autolayout Ignores Multi-Line Detailtextlabel When Calculating Uitableviewcell Height (All Styles)
Using Uisheetpresentationcontroller in Swiftui
Regex to Get String Between Two % Characters
How to Set the Accessibility Label for a Particular Segment of a Uisegmentedcontrol