Default tableview cells don't respond to dark mode - swift

Using a custom cell I'm able to get dark mode/normal mode to work properly. But when using the default framework cell Apple has provided it remains white regardless of what mode I enable. I read here
ios13 Dark Mode change not recognized by tableview Cell?
about the same problem. The answer tells me to use this:
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
removeAndReaddGradientIfNeeded()
}
}
But I'm unsure how exactly I'm supposed to use this and how it relates to my cells. My code right now for my cells is this:
if #available(iOS 13, *) {
cell.backgroundColor = UIColor.systemBackground
cell.textLabel?.textColor = UIColor(named: "MainLabelColor")
cell.detailTextLabel?.textColor = UIColor(named: "SubLabelColor")
}
I use system color and custom colors in assets with two modes, one for light and one for dark. Now, this works fine in custom cell, but not in default.
Could anyone show me how to use the delegate function with cells?

Did you try to change the contentView background color? because the content view sits on top of the cell.
if #available(iOS 13, *) {
cell.contentView.backgroundColor = UIColor.systemBackground
//For named color you have to resolve it.
cell.textLabel?.textColor = UIColor(named: "MainLabelColor")?.resolvedColor(with: self.traitCollection)
cell.detailTextLabel?.textColor = UIColor(named: "SubLabelColor")?.resolvedColor(with: self.traitCollection)
//MARK:- Even If your Viewcontroller disabled dark mode, tableView cell will be enabled.
self.overrideUserInterfaceStyle = .unspecified
}
To Support Dark Mode make sure you removed following overrides:-
UserInterfaceStyle default value is unspecified . So, You might have enabled userInterfaceStyle to light in somewhere in your code or list file.
In Plist file check for following key-value and remove them:-
<key>UIUserInterfaceStyle</key>
<string>light</string>
In Code check for the following the line and remove them.
i) If the key window is overridden to light mode, your entire app will be forced to light mode.
UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .light
ii) If View Controller is overridden to light mode, your entire ViewController will be forced to light mode.
self.overrideUserInterfaceStyle = .light

Related

White background in table section header in iOS14

This issue appeared after building to iOS14 with xcode12.
I have a section header with transparent background, on iOS14 it becomes white with new _UISystemBackgroundView added to the hierarchy.
iOS 14 comes with new two cell configurations:
Content configurations. UIContentConfiguration
As the name suggests, content configurations can help you manipulate the content of the cell like image, text, secondary text, layout metrics and behaviors.
Background configurations UIBackgroundConfiguration
can help with the manipulation of background color, visual effect, stroke, insets and corner radius. All cells will inherit a default background configuration even if we don’t specify one.
The Solution
To get rid of the default iOS14 white background you need to change the UITableViewCell or UITableViewHeaderFooterView backgroundConfiguration as follows
// Add this code in your AppDelegate didFinishLauncingWithOptions
// or you can change configuration of certain subclass using self. backgroundConfiguration = ...
if #available(iOS 14.0, *) {
var bgConfig = UIBackgroundConfiguration.listPlainCell()
bgConfig.backgroundColor = UIColor.clear
UITableViewHeaderFooterView.appearance().backgroundConfiguration = bgConfig
//For cell use: UITableViewCell.appearance().backgroundConfiguration = bgConfig
}
Read this article for more
In your UITableViewHeaderFooterView / UITableViewCell custom class - override next method with implementation example:
Swift:
#available(iOS 14.0, *)
override func updateConfiguration(using state: UICellConfigurationState) {
backgroundConfiguration = UIBackgroundConfiguration.clear()
}
Objective-C:
- (void)updateConfigurationUsingState:(UICellConfigurationState *)state {
self.backgroundConfiguration = [UIBackgroundConfiguration clearConfiguration];
}
Objective-C version of #Husam solution:
if (#available(iOS 14.0, *)) {
UIBackgroundConfiguration *bgConfig = [UIBackgroundConfiguration listPlainCellConfiguration];
bgConfig.backgroundColor = UIColor.clearColor;
[UITableViewHeaderFooterView appearance].backgroundConfiguration = bgConfig;
}
Use iOS 14's configuration based APIs may disable the functions of those legacy APIs (e.g. cell.textLabel, cell.detailTextLabel).
To prevent this system behavior, you can set a backgroundView (legacy API) to your header/footer/cell, and then set a custom backgroundColor for that view.

How to implement DarkMode Into app in Swift

sorry if that question was asked but couldn't find the right answer across stackOverFlow so I'm asking ..
I'm trying to implement dark mode into my app, but unfortunately it doesn't work well for me while using tableviews, it does changes my background and stuff, but I can't change the color of my groups in my tableview.
Here's an image to illustrate the problem:
https://imgur.com/a/h4A3zOZ (can't upload it here cause its too big).
Also Here is my Code:
// MARK: - Premium Section - DarkMode + Graph:
#IBAction func darkModeSwitch(_ sender: UISwitch) {
let current = sender.isOn ? Theme.dark : Theme.light
if #available(iOS 13.0, *) {
// overrideUserInterfaceStyle = UIUserInterfaceStyle(rawValue: current.stateMode)!
//STEP1: Saving User Defaults Switcher:
saveSwitchToggleDarkMode(switcherState: sender.isOn)
//STEP2: Setting UI Colors Of Settings View:
self.tableView.backgroundColor = current.backgroundColor
///Setting up the barTint Color:
self.navigationController?.navigationBar.barTintColor = current.barTintColor
///Setting up the title text color:
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:current.textColor]
///Changing back color in navigation controller:
self.navigationController?.navigationBar.backItem?.backBarButtonItem?.tintColor = current.backItemColor
}
}
You should change the mode on the window's level to apply changes to all your controls e.g:
if #available(iOS 13, *) {
UIApplication.shared.delegate?.window??.overrideUserInterfaceStyle = .dark
}
An alternative (and perhaps easier) method to implement dark mode is to use the iOS dark mode feature that you can trigger in settings.If you want to implement this you can create a custom color set by going to your Assets.xcassets and pressing the plus mark on the bottom -> new color set. On the attributes inspector, name your color under name, and under Appearances, select 'Any, Light, Dark' now you will have a place for 3 different colors. Under Light, put the light mode color, on the dark, the dark mode color.
Then on the place where you wish to implement this color,you can change the color to your custom color in the storyboard like so :-
or you can change it in code with something like
myButton.backgroundColor = UIColor(named: "TestColor")
When the user triggers the Dark mode through their control center or settings, the app will also automatically change accordingly. You can test this by going to settings -> Developer -> Dark appearance or by going to Features -> Toggle Appearance or simply press Shift + Command + A
However this method means that you will not have an independent dark mode because it will only be triggered if the device itself is dark-mode enabled.

Transparent NSCollectionView Background

I'm again struggling with setting NSViews background colors to transparent. I have a NSCollectionView as part of NSClipView which is part of a NSScrollview. My MainViewController has an outlet to the collectionView. With adding the two lines of code and after compiling the background is sometimes transparent but most of the times not:
view.wantsLayer = true
collectionView.layer?.backgroundColor = NSColor.clear.cgColor
I also tried to select/de-select the "Draw Background" property of the NSScrollView in the IB without any effects.
What do I miss here.
I struggled a bit attempting to get my NSCollectionView background "transparent" #JFS solution pointed me in the right direction: and I finally achieved it by setting both the parent scrollView and the collectionView backgrounds:
cvScrollView.backgroundColor = NSColor.clear
collectionView.backgroundColors = [NSColor.clear]
Ok, after a long evil trial and error phase I found a solution at least for myself. There are two background colors to set in order to have the proper behavior:
the background color of the NSScrollView:
the NSCollectionView primary color:
Both have to be set appropriately. At the point I set both to the same color I got the background I want. With setting only the ScrollView background color there was still the white background when scrolling the items in the CollectionView.
To make clear background for NSScrollView, the best option in Swift 4.2 is "Not draw a background". Let's get to view a programmatically example:
let scrollView = NSScrollView()
scrollView.drawsBackground = false
scrollView.contentView.drawsBackground = false
NSCollectionView has background for sections, so you need to specify colors for sections
let collectionView = NSCollectionView()
collectionView.backgroundColors = [.clear]
If you set backgroundColors to nil or to empty array, the background color is set by default to white.
If you set a background view for NSCollectionView, this array is ignored
You could try to put a NSView with frame zero as a backgroundView for NSCollectionView

UINavigationBar titleTextAttributes does not update fully after reloading views

This is a tricky one.
Here is my storyboard for this demo:
The Settings screen segues to the My Color screen where users can choose either a dark or light color scheme for the app. When a change is made, I remove all views from the window and then re-add them to force the current view to immediately apply the changes via the UIAppearance proxy. So the color of the navigation bar and the nav bar's text color both change immediately.
Next, the user unwinds the segue to return to the Settings screen. On the Settings screen, the new color of the navigation bar is already applied. The new color of the nav bar's text is also already applied. However, for a brief instant while the segue is in transition, the nav bar still shows the old text color. The new text color is not shown until after the transition is complete. This results in a minor, but noticable, visual glitch as the nav bar's text suddenly changes from the old color to the new color.
To update the color of the nav bar text when the user flips the switch, I run the following code in the My Color screen's view controller. (The full project code up on Github at https://github.com/prinomen/social_demo2).
func switchValueDidChange(sender:UISwitch!) {
if (sender.on == true) {
colorIndex = 1 // nav bar is now black
UINavigationBar.appearance().barTintColor = black // set appearance proxy to the new color
// Run this switch to set the textColor global var to match the preferred color scheme, based on the value of colorIndex.
switch colorIndex {
case 0: // white
textColor = green
statusBarTextIsBlack = true
case 1: // black
textColor = red
statusBarTextIsBlack = false
default:
break;
}
// Update these appearance proxy items (they need the window to reload before they will manifest their changes).
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir-Medium", size: 22)!, NSForegroundColorAttributeName: textColor]
UINavigationBar.appearance().tintColor = textColor
// Remove all views from the window and then re-add them in order to force the current view to immediately apply changes to UIAppearance.
let windows : NSArray = UIApplication.sharedApplication().windows
for window in windows as! [UIWindow] {
for view in window.subviews {
view.removeFromSuperview()
window.addSubview(view)
}
}
} else {
colorIndex = 0 // nav bar is now white
UINavigationBar.appearance().barTintColor = white // set appearance proxy to the preferred color
// Run this switch to set the textColor global var to match the preferred color scheme, based on the value of colorIndex.
switch colorIndex {
case 0: // white
textColor = green
statusBarTextIsBlack = true
case 1: // black
textColor = red
statusBarTextIsBlack = false
default:
break;
}
// Update these appearance proxy items (they need the window to reload before they will manifest their changes).
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir-Medium", size: 22)!, NSForegroundColorAttributeName: textColor]
UINavigationBar.appearance().tintColor = textColor
// Remove all views from the window and then re-add them in order to force the current view to immediately apply changes to UIAppearance.
let windows : NSArray = UIApplication.sharedApplication().windows
for window in windows as! [UIWindow] {
for view in window.subviews {
view.removeFromSuperview()
window.addSubview(view)
}
}
}
}
Aside from changing the color via the appearance proxy, I've also tried setting the color explicitly within the viewWillAppear and viewWillLayoutSubviews methods of the Settings screen view controller by running this line:
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir-Medium", size: 22)!, NSForegroundColorAttributeName: textColor]
But this results in the same issue. What I find confusing is that the other changes made via the appearance proxy are updated without encountering this issue. Only the titleTextAttributes property is troubled by this issue.
I thought that maybe iOS makes some kind of "snapshot" of the Settings screen when segueing to the My Color screen. Then when the segue is reversed, the "snapshot" with the old nav bar text color is used and the new color is not updated until the segue is finished. But if that were true, then why doesn't the navigation bar's barTintColor also experience the same problem? There must be a different way the reverse segue is handled, but I can't seem to figure it out.
Is there a way to apply the color change to the title text before the transition happens, in a way that affects the transition itself?
Thanks for any insight!

ContentView background colour Swift

When I select a cell on a table view it by default changes it's ContentView background colour to a light grey and I am trying to override this.
And I am using the following code to remove this grey colour from the selection. But it is removing the background colour of everything (UIViews, UIImageViews) inside that cell.
Is there a way to eliminate the background colour of the ContentView only?
let changeselectionColor = UIView()
changeselectionColor.backgroundColor = UIColor.clearColor()
cell.selectedBackgroundView = changeselectionColor
I have the following table view cell.
This is how it should look like when selected
This is how it end up looking with all BGs transparent
You don't need to override anything to achieve the effect, actually UITableViewCell provides the API to remove the highlight gray:
cell.selectionStyle = UITableViewCellSelectionStyle.None