SwiftTheme icon indicating copy to clipboard operation
SwiftTheme copied to clipboard

UITabBarItem theme_setTitleTextAttributes crash

Open Auk513 opened this issue 3 years ago • 3 comments

I want to set UITabBarItem titleTextAttributes from Plist. Copy the code from UINavigationBar.apperance() in the Plist Demo.

// tab bar item
        let tabBarItem = UITabBarItem.appearance()
        tabBarItem.theme_setTitleTextAttributes(ThemeStringAttributesPicker(keyPath: "Global.barTextColor") { value -> [NSAttributedString.Key : AnyObject]? in
            guard let rgba = value as? String else {
                return nil
            }

            let color = UIColor(rgba: rgba)
            let shadow = NSShadow(); shadow.shadowOffset = CGSize.zero
            let titleTextAttributes = [
                NSAttributedString.Key.foregroundColor: color,
                NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16),
                NSAttributedString.Key.shadow: shadow
            ]

            return titleTextAttributes
        }, forState: UIControl.State.normal)

Then it crash.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Have you sent -theme_setTitleTextAttributes:forState: to <UITabBarButtonLabel: 0x7fad8c407f00; frame = (0 0; 50.3333 12); text = 'PlistDemo'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x600003f84c80>> off the main thread? To verify, look for a complaint in the logs: "Unsupported use of UIKit…", and fix the problem if you find it. If your use is main-thread only please file a radar on UIKit, and attach this log. exercisedImplementations = {
    "theme_setTitleTextAttributes:forState:" =     (
    );
}'

Auk513 avatar Mar 10 '21 03:03 Auk513

Same issue I fixed this by observing theme and setting setTitleTextAttributes by my own

aarashgoodarzi avatar Mar 12 '21 19:03 aarashgoodarzi

I didn't find the real reason. I tested UITabBarItem.appearance().theme_setTitleTextAttributes(...) and it would crash, but UITabBarItem().theme_setTitleTextAttributes(...) would not crash. I guessed inside the object returned by UITabBarItem.appearance() There is some unknown logic blocking this. I don’t have a solution right now, and I will come back to this issue in the future.

wxxsw avatar Apr 17 '21 11:04 wxxsw

https://stackoverflow.com/questions/42677534/swizzling-on-properties-that-conform-to-ui-appearance-selector @wxxsw for the reference

linlinyao1 avatar Oct 27 '21 11:10 linlinyao1