HomeiOS Developmentios - Is it doable to vary a customized trait from a...

ios – Is it doable to vary a customized trait from a mother or father trait assortment and observe the modifications to all view controllers?


I am making an attempt to create a customized trait object and put it into the UITraitCollection class utilizing some new APIs in iOS 17. For example, what I need to do is create a number of app theme traits, put them into the trait assortment class, and make the entire present obtainable UIViewController objects to watch the theme trait modifications. I am conscious that in iOS 17, UIKit has a brand new technique to override a trait definition by mutating the brand new traitOverrides property. However based mostly on the WWDC23 video session, Apple instructed us to make use of this property just for particular view controllers or views and all of its kids/subviews. So the modifications do not mirror to the entire view controllers which can be at present obtainable in reminiscence. This is what I did to date:

Creating a brand new customized UITraitDefinition:

enum ThemeTraitType: Int {
    case gentle
    case darkish
    case monochrome
}

struct ThemeTrait: UITraitDefinition {
    typealias Worth = ThemeTraitType
    
    static let defaultValue: ThemeTraitType = .gentle
    static var identify: String = "Theme"
    static var affectsColorAppearance: Bool = true
}

Added it into the UITraitCollection class utilizing extensions:

extension UITraitCollection {
    var currentTheme: ThemeTraitType { self[ThemeTrait.self] }
}

extension UIMutableTraits {
    var currentTheme: ThemeTraitType {
        get { self[ThemeTrait.self] }
        set { self[ThemeTrait.self] = newValue }
    }
}

Register the trait modifications observer:

closing class ViewController: UIViewController {
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        registerForTraitChanges([ThemeTrait.self], handler: { (self: Self, previousTraitCollection: UITraitCollection) in
            // Do one thing right here...
        })
    }

}

Change the currentTheme trait utilizing the brand new traitOverrides property:

traitOverrides.currentTheme = .darkish

This works as anticipated for the view controller the place I alter the trait from. However for instance I am utilizing a UINavigationController and have 5 view controllers contained in the navigation controller and I alter the trait contained in the final view controller (which is the fifth view controller contained in the stack), once I return to the earlier view controller, the trait modifications would not mirror on the entire earlier view controllers. What I need to do is the modifications I’ve made inside any view controllers can even mirror to the entire obtainable view controllers that at present exist in reminiscence. Is that this doable to implement? Thanks.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments