Set UISearchBar Background Color to White - ios

I am facing issue to set bg color of UISearchBar as White. I have tried to set bg as white but still default gray bg color is there.
I tried this code.
searchController?.searchBar.searchTextField.backgroundColor = UIColor.white
I dont understand what's wrong. Bcz when i set any color like green or red, they are set easily.
Please provide answer in Swift 4+ .

Use the below in Appdelegate
if #available(iOS 13, *) {
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = . white
}

We can't set background color of SearchBar as white.
As we can see all Apple default Apps doesn't have white BG color,
Solution: We need to make customised SearchBar for that.

Related

Setting UISearchbar background color to white in IOS

I am trying to change the background color of the UISearchbar. I am able to set the background to any color but white. Not sure why white does not get set. I am using the below code snippets to set the color. I have an extension method to get the textField. So, you can assume that textField exists.
searchBar.backgroundColor = .white
searchBar.textField?.backgroundColor = .white
It always has gray color when I set it to white.
However, if I use a color other than white it shows up that color.
searchBar.textField?.backgroundColor = .blue
Any idea what is wrong here.
The only reliable way I have found to do this is to set the searchTextField's .borderStyle to .none and manually set the layer's cornerRadius to match what the default border style would be. Something like:
self.searchBar.searchTextField.borderStyle = .none
self.searchBar.searchTextField.layer.cornerRadius = 10
self.searchBar.searchTextField.backgroundColor = .white
Try this one:
if #available(iOS 13.0, *) {
searchController.searchBar.textField.backgroundColor = .white
}

iOS programmatically change all default colours

This code in applicationDidFinishLaunchingWithOptions()
window?.tintColor = .red
window?.backgroundColor = .green
change the default views tint color, not the background color, of my whole application, which is a double view with table view.
The default text color is a black "Color Dark Text" in label texts, and a black "Color default" in text fields.
Is there a way to programmatically change all the defaults color, foreground and background?
Yes, you can do it via appearance. E.g. to change backround color of every view:
UIView.appearance().backgroundColor = .green
Note: If you change background of some view, appearance will not apply.

Tint Color works with native colors but not with the color from pattern image

I am theaming the navigation bar back button with the navigation bar tint color it works when I use native colors and it fails when I use custom color from an image.
working code :
self.navigationController?.navigationBar.tintColor = UIColor.orange
Not working code:
let color = UIColor.init(patternImage: someImage)
self.navigationController?.navigationBar.tintColor = color
Can someone show some light?
You cannot use pattern color for tint color.
Refer the following link:
https://developer.apple.com/documentation/uikit/uiview/1622467-tintcolor
look for this note:
Important
If you attempt to use a pattern color as a tint color, the system
raises an exception.
If you have a image then please set image in navigationbar as a background image. Like :
self.navigationController?.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "NavigationBackground"), for: .default)
Hope this is helpful.

Change statusBar background color while using large title for navigation bar on iOS 11

I'm trying to use the new navigationBar's large title feature on iOS 11.
However, after I added the following line:
self.navigationController?.navigationBar.prefersLargeTitles = true
I found that the navigationBar background color changed to black.
So I set background color again manually:
self.navigationController?.setBackgroundColor(UIColor(hexString: 0xFF7E79))
However, I found that the statusBar background color didn't change:
After I set up the background color of statusBar through this code:
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return
statusBar.backgroundColor = UIColor(hexString: 0xFF7E79)
It gives me an ugly 1px black line like this between the statusBar and the navigationBar:
So what is the correct way to set the background color of navigationBar?
The correct way to set the background color of the UINavigationBar is to use the barTintColor property.
self.navigationController?.navigationBar.barTintColor = .red
You may notice that the color you set can be a little faded. As noted in the documentation:
This color is made translucent by default unless you set the isTranslucent property to false.
See the barTintColor reference on developer.apple.com

UISegmentedContol: reverse tintColor and backgroundColor

I have UISegmentedControl that I add a few white images on transparent background to.
for (index,element) in ELEMENTS.enumerate() {
segmentedControl.insertSegmentWithImage(element.logo, atIndex: index, animated: false)
}
Segments not selected now have the background color set to segmentedControl.backgroundColor, and the image is colored with segmentedControl.tintColor. The selected segment is reversed, with the background set to .tintColor, and the image colored with the .backgroundColor.
This works fine, but I would like it to be the other way around: That the selected segment has a image colored with .tintColor, and background colored .backgroundColor.
I know I can achieve this by just switching the colors in code, but I'm using
let sharedApplication = UIApplication.sharedApplication()
sharedApplication.delegate?.window??.tintColor = newColor
in the app to change the tintColor of all the views in the app, so it would be nice if this would result in the color being changed the way I want it in my segmented control.
Any ideas?
You use UIApplication.sharedApplication().delegate?.window??.tintColor to set global tint color that is used by all controls of your application.
You can use UISegmentedControl.appearance().tintColor to set custom tint color for all segmented controls in your application.
And you can use UISegmentedControl.tintColor to set custom tint color for specific segmented control.
To switch background and tint colors for all segmented controls in your application:
UISegmentedControl.appearance().tintColor = backgroundColor
UISegmentedControl.appearance().backgroundColor = tintColor

Resources