Cannot implement iOS 13 Darkmode

For the past few days, I was trying to implement iOS 13 Darkmode without success. When I dismiss the scene by dragging it across the screen, the method traitCollectionDidChange would be called several times, all with its own set of values. The issue is that this causes the UI to flicker between dark and light mode.
Here are the codes below to get an idea of what I am dealing with.

IMPLEMENTATION
func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)

print("THEME instance: \(self)")  

let currentTraitCollection = self.traitCollection  
var hasUserInterfaceStyleChanged = false  
hasUserInterfaceStyleChanged = previousTraitCollection.hasDifferentColorAppearanceCompared(to: currentTraitCollection)  
  
print("THEME hasUserInterfaceStyleChanged = \(hasUserInterfaceStyleChanged ? "YES" : "NO")")  
  
if hasUserInterfaceStyleChanged {  
    let userInterfaceStyle = currentTraitCollection.userInterfaceStyle // Either .unspecified, .light, or .dark  
      
    switch userInterfaceStyle {  
        case .unspecified:  
            print("THEME UIUserInterfaceStyleUnspecified")  
        case .light:  
            print("THEME UIUserInterfaceStyleLight")  
        case .dark:  
            print("THEME UIUserInterfaceStyleDark")  
        }  
} else {  
    print("THEME NOT CHANGED")  
}  

}

NEW SCENES
THEME instance: <MainControllerViewController: 0x117e55910>
THEME hasUserInterfaceStyleChanged = YES
THEME UIUserInterfaceStyleLight

WHEN SCENE GOES AWAY
THEME instance: <MainControllerViewController: 0x117e55910>
THEME hasUserInterfaceStyleChanged = YES
THEME UIUserInterfaceStyleDark
THEME instance: <MainControllerViewController: 0x117e55910>
THEME hasUserInterfaceStyleChanged = YES
THEME UIUserInterfaceStyleLight
THEME instance: <MainControllerViewController: 0x117e55910>
THEME hasUserInterfaceStyleChanged = NO
THEME NOT CHANGED
THEME instance: <MainControllerViewController: 0x117e55910>
THEME hasUserInterfaceStyleChanged = YES
THEME UIUserInterfaceStyleDark
THEME instance: <MainControllerViewController: 0x117e55910>
THEME hasUserInterfaceStyleChanged = YES
THEME UIUserInterfaceStyleLight

hey there!
check the information in this link

https://medium.com/ios-expert-series-or-interview-series/implementing-dark-mode-in-ios-13-54eb6775a1b7