UISegmentedContol: reverse tintColor and backgroundColor - ios

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

Related

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.

Separate colors for viewcontroller background and statusbar background

I want a blue color for my navigation bar and status bar backgrounds while a white color background for the rest of the view controller. I want this for all my view controllers in the app. Naturally, I have in my BaseViewContrller for statusbar:
self.view.backgroundColor = UIColor.blue
But this causes the whole view controller to go blue. Is adding a white UIView and constraining it to safe areas the only option or there is an easy way to accomplish this?
Yes, because you're setting ViewController's view background color and not navigationBar background color. Instead set background color of navigation bar
navigationController?.navigationBar.backgroundColor = .blue

How to change the color of AVPlayerViewController buttons background color

I am using the AVPlayerViewController to play video. To change the background color of buttons' controls background view as highlighted in below screenshot:-
When I tried to change the color as:-
self.contentOverlayView?.backgroundColor = UIColor.brown
then the whole view's color get changes as below:-
How to change the color of top and bottom controls' background view.

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

How to change iOS app's background image?

I want to change my app's whole background image to black, including the status bar and navigation bar, because I want to use black underpainting and gray font color. How can I do this?
You can change the view into UIButton, UILabel, UIImageView, etc.
view.backgroundColor = [UIColor blackColor];

Resources