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
Change UISearchBar textColor not working
You need to create an extension like follwoing:
public extension UISearchBar {
public func setNewcolor(color: UIColor) {
let clrChange = subviews.flatMap { $0.subviews }
guard let sc = (clrChange.filter { $0 is UITextField }).first as? UITextField else { return }
sc.textColor = color
}
}
And change color using following code:
controller.searchBar.setNewcolor(UIColor.redColor())
Output is :
Update:
For the change color of searchBar text for the GMSAutocompleteViewController
you need to do following code:
let searchBarTextAttributes: [String : AnyObject] = [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())]
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes
That change the text out put like following image:
And if you wish to change placeholder text and it's color for searchBar
. You need to do following code:
let placeholderAttributes: [String : AnyObject] = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())]
let attributedPlaceholder: NSAttributedString = NSAttributedString(string: "Find a place", attributes: placeholderAttributes)
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).attributedPlaceholder = attributedPlaceholder
It will be show like:
How to change UISearchBar Font Size & Color?
To change text and placeholder searchbar:
// SearchBar text
let textFieldInsideUISearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideUISearchBar?.textColor = UIColor.red
textFieldInsideUISearchBar?.font = textFieldInsideUISearchBar?.font?.withSize(12)
// SearchBar placeholder
let labelInsideUISearchBar = textFieldInsideUISearchBar!.value(forKey: "placeholderLabel") as? UILabel
labelInsideUISearchBar?.textColor = UIColor.red
Change text color of search bar ios
firstly, you find the subview in UISearchBar
, then find the UITextField
in sub View then change color
Try this code:-
for(UIView *subView in searchBar.subviews){
if([subView isKindOfClass:UITextField.class]){
[(UITextField*)subView setTextColor:[UIColor blueColor]];
}
}
for Ios 5 +
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];
SearchBar, how to change text color?
Its a little hard to access the Textfield inside the UISearchbar.
This is how it works:
for subView in self.searchBarOutlet.subviews
{
for secondLevelSubview in subView.subviews
{
if (secondLevelSubview.isKindOfClass(UITextField))
{
if let searchBarTextField:UITextField = secondLevelSubview as? UITextField
{
//set the color here like this:
searchBarTextField.textColor = UIColor.redColor()
break;
}
}
}
}
Shorter solution:
var textFieldInsideSearchBar = yourSearchbar.valueForKey(“searchField”) as? UITextField
textFieldInsideSearchBar?.textColor = yourcolor
How to change background color of the text field in the UISearchController?
Here is a an example on how to set the textField
background.
class ViewController: UIViewController {
let searchController = UISearchController(searchResultsController: nil)
private lazy var searchTextField: UITextField? = { [unowned self] in
var textField: UITextField?
self.searchController.searchBar.subviews.forEach({ view in
view.subviews.forEach({ view in
if let view = view as? UITextField {
textField = view
}
})
})
return textField
}()
override func viewDidLoad() {
super.viewDidLoad()
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Candies"
navigationItem.searchController = searchController
definesPresentationContext = true
if let bg = self.searchTextField?.subviews.first {
bg.backgroundColor = .green
bg.layer.cornerRadius = 10
bg.clipsToBounds = true
}
}
}
Result
search bar text field background color swift
You can simply set the barTintColor
of the UISearchBar
instance to your required color, i.e.
searchBarOutlet.barTintColor = .white
And use backgroundColor
change the background color of the searchTextField
searchBarOutlet.searchTextField.backgroundColor = .white
So, the below code works fine as per your requirement
searchBarOutlet.layer.cornerRadius = 6
searchBarOutlet.layer.masksToBounds = true
searchBarOutlet.layer.borderWidth = 1.5
searchBarOutlet.layer.borderColor = UIColor.init(hexFromString: "7E5BEB").cgColor
searchBarOutlet.isTranslucent = true
searchBarOutlet.barTintColor = .white
searchBarOutlet.searchTextField.backgroundColor = .white
Screenshot:
Related Topics
Uicollectionview with a Sticky Header
Find Where Object Is Retained with Arc
How to Build .Ipa for React Native
iOS App Crashing Every Other Launch, Can't Find Error
Nsuserdefaults Unreliable in iOS 8
How to Get the Hour of the Day with Swift
iOS Present Modal View Controller on Startup Without Flash
Change App Version with Only IPA File Provided (No Xcode)
How to Set Multi Line Large Title in Navigation Bar? ( New Feature of iOS 11)
How to Add Done Button on Keyboard on Top of Keyboard in iOS
Frosted Glass (iOS 7 Blur) Effect
Facebook iOS Sdk - Get Friends List
Logging Method Signature Using Swift
How to Add Constraint Between a View and the Top Layout Guide in a Xib File
Accessing the Host App Code from the Xcode 7 UI Test Target
New React Native Project with Old Version of React Native