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.
Related
My application has a setting screen that allows the user to change app color including navigation bar and tab bar tint colors and also allow him to change app font and font size for all texts in the app like in Telegram application. the problem is when user select a color for a list of specific colors I need something like reloads or refreshes my application to make the change affect the tab bar and navbar of the application
I found this question but it changes the color one time when the app launched but I want to allow the user to customize the application color and font
Changing navigation bar color in Swift
After a long search, I found the answer to fix the problem of the change navigation bar and tab bar color, tint color, and font in run time when the user chooses a specific color.
to change the color of the navigation bar:
use this to change the color of navbar for the current screen
navigationController?.navigationBar.barTintColor = Color
and that for changing the color for navbar in the entire app
UINavigationBar.appearance().tintColor = Color
UINavigationBar.appearance().barTintColor = Color
and this for changing navbar font for the current screen
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: AppFont().large]
and this for the entire app
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: AppFont().large]
and this for change tab bar tint color
self.tabBarController?.tabBar.tintColor = Color.theme.value
and this to change tab bar items font
let selectedAttrs = [NSAttributedString.Key.font: Font, NSAttributedString.Key.foregroundColor: Color]
if let items = self.tabBarController?.tabBar.items {
for item in items {
item.setTitleTextAttributes(selectedAttrs, for: .selected)
}
}
https://gph.is/g/ZOR7bAP
My UIBarButtonItem image doesn't change its color when switching between dark and light mode.
I set the color programmatically and expected it to change between black and white when switching the mode.
At least it works with the tintColor of my NavigationBar.
I set:
myBarButton.tintColor = UIColor.white
and the image of the button stays white in dark AND in light mode.
On the other hand, the following is black in light mode and white in dark mode:
navigationBar.tintColor = UIColor.white
Why does it behave differently and how can I add this functionality to my UIBarButtonItem?
UIColor.white is not a dynamic color. It will be white regardless of the appearance setting. If you want a color that is different depending on the appearance, you need to take one of the new dynamic system colors (e.g., UIColor.systemBackground would be white in light and black in dark mode), or create a color asset with different color values for light and dark appearance in an asset catalog.
Here is more on the new system colors: https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/color#dynamic-system-colors
Using iOS 13's new appearance API:
https://developer.apple.com/documentation/uikit/uinavigationbarappearance
Example:
let navStyle = UINavigationBarAppearance()
let buttonStyle = UIBarButtonItemAppearance()
// Change properties of buttonStyle here with dynamic colours such as UIColor.label.
style.buttonAppearance = buttonStyle
style.doneButtonAppearance = ...
style.backButtonAppearance = ...
navigationController?.navigationBar.standardAppearance = navStyle
navigationController?.navigationBar.scrollEdgeAppearance = ...
navigationController?.navigationBar.compactAppearance = ...
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
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
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];