tintColor not updating to default in Theme
tintColor not updating to default in Theme
I have a custom icon (.png image) as a barbutton and I'm going to be using a Theme.swift file to set asset colors/background color for 1 particular view controller but for some reason the tint color is not updating.
It's currently the dark grey color used when it was added to my Assets folder, but should show up as a green color as per my Theme.swift. Does anyone know of a reason why this might not be updating to the new default color of green:
In the viewDidLoad of the viewcontroller I'm looking to theme, I have added
Theme.current.apply()
And this is my Theme.swift file with a default green color:
import UIKit
enum Theme: Int
//1
case `default`, dark, graphical
//2
private enum Keys
static let selectedTheme = "SelectedTheme"
//3
static var current: Theme
let storedTheme = UserDefaults.standard.integer(forKey: Keys.selectedTheme)
return Theme(rawValue: storedTheme) ?? .default
var mainColor: UIColor
switch self
case .default:
print("/default")
return UIColor(red: 87.0/255.0, green: 188.0/255.0, blue: 95.0/255.0, alpha: 1.0)
case .dark:
return UIColor(red: 255.0/255.0, green: 115.0/255.0, blue: 50.0/255.0, alpha: 1.0)
case .graphical:
return UIColor(red: 10.0/255.0, green: 10.0/255.0, blue: 10.0/255.0, alpha: 1.0)
func apply()
//2
UIApplication.shared.delegate?.window??.tintColor = mainColor
/default even prints in my console, but the color does not update.
2 Answers
2
Here, problem with image renderingMode
. If you want to apply tintColor
on image you have to use image rendering mode as alwaysTemplate
.
renderingMode
tintColor
alwaysTemplate
Image rendering mode can be set using two methods.
Programmatically
if let myImage = UIImage(named: "myImage")
let tintableImage = myImage.withRenderingMode(.alwaysTemplate)
imageView.image = tintableImage
From Asset
Change to
alwaysTemplate
– Pratik Sodha
Sep 3 at 6:25
alwaysTemplate
Oh! I misread your post. Ok now its Template image but still not working in simulator. Anything else I can look at?
– bhealth
Sep 3 at 6:30
hmm.. Try to apply static
tintColor
from storyboard for now. It's easy to debug.– Pratik Sodha
Sep 3 at 6:32
tintColor
@bhealth Your
Theme
class working for me.. As your exception.– Pratik Sodha
Sep 3 at 7:40
Theme
Try to change NavigationBar tint color
NavigationBar tint color
Sample code for change navigation bar tint color
UINavigationBar.appearance().tintColor = UIColor.purple //your selected color
Sample for your apply method please check it
func apply()
UIApplication.shared.delegate?.window??.tintColor = mainColor
// change navigation bar tint colour
UINavigationBar.appearance().tintColor = mainColor // or your selected color
The NavigationBar tint color also doesn't change with apply method as you suggested
– bhealth
Sep 3 at 7:22
@Moritz I agree with you but this code is just for this particular answer, you are not consider for all projects and all scenario, In this code "
bhealth
" will save selected theme in UserDefault
thats why I wrote UserDefault
code, But I have remove UserDefault
code in my answer– Vivek
Sep 3 at 9:28
bhealth
UserDefault
UserDefault
UserDefault
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Hey, I just check my asset and its showing render: default - imgur.com/a/9qKHsum
– bhealth
Sep 3 at 6:23