SwiftTheme
SwiftTheme copied to clipboard
UITabBarItem theme_setTitleTextAttributes crash
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:" = (
);
}'
Same issue I fixed this by observing theme and setting setTitleTextAttributes by my own
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.
https://stackoverflow.com/questions/42677534/swizzling-on-properties-that-conform-to-ui-appearance-selector @wxxsw for the reference