wxxsw / SwiftTheme

🎨 Powerful theme/skin manager for iOS 9+ 主题/换肤, 暗色模式
MIT License
2.52k stars 306 forks source link

[iOS 13] Support for UISegmentedControl setTitleTextAttributes #106

Closed n3d1117 closed 5 years ago

n3d1117 commented 5 years ago

Hello, could you add support for the new method setTitleTextAttributes:for: that was added in UISegmentedControl with iOS 13? Thanks

wxxsw commented 5 years ago

No problem, I will support this weekend.

wxxsw commented 5 years ago

Example:

segmentedControl.theme_setTitleTextAttributes([
    [.foregroundColor: UIColor.purple],
    [.foregroundColor: UIColor.blue],
], forState: .normal)
n3d1117 commented 5 years ago

Thank you! However my app crashes when I use it. Any idea why?

Here's the crash log:

2019-09-28 15:52:39.603610+0200 appdb[95960:2027051] -[__NSCFNumber _ui_attributesForDictionaryContainingUIStringDrawingKeys]: unrecognized selector sent to instance 0xe51b635bdae73ea0
2019-09-28 15:52:39.635129+0200 appdb[95960:2027051] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber _ui_attributesForDictionaryContainingUIStringDrawingKeys]: unrecognized selector sent to instance 0xe51b635bdae73ea0'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23b98bde __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff503b5b20 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff23bb9704 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x00007fff23b9d7bc ___forwarding___ + 1436
    4   CoreFoundation                      0x00007fff23b9f6c8 _CF_forwarding_prep_0 + 120
    5   UIKitCore                           0x00007fff46b769e1 -[UISegmentedControl _setTitleTextAttributes:forState:] + 133
    6   UIKitCore                           0x00007fff46b76ac6 -[UISegmentedControl setTitleTextAttributes:forState:] + 53
    7   SwiftTheme                          0x00000001043252e1 $sSo8NSObjectC10SwiftThemeE07performC6Picker8selector6pickerySS_AC0cE0CSgtFTf4xnn_n + 2017
    8   SwiftTheme                          0x000000010433a676 $s10SwiftTheme03setB6Picker33_D6D88D627DB5A18224BA0B71F9E3B89DLLyySo8NSObjectC_SSAA0bD0CSgtFTf4nxn_n + 662
    9   SwiftTheme                          0x0000000104337c85 $sSo8UIButtonC10SwiftThemeE14theme_setImage_8forStateyAC0cF6PickerCSg_So09UIControlH0VtFToTm + 101
    10  appdb                               0x000000010364c208 $s5appdb21DetailsSegmentControlC_5state7enabled8delegateACSayAA0b8SelectedC5StateOG_AHSbAA06SwitchbC8Delegate_ptcfC + 4008
    11  appdb                               0x00000001035cc5d2 $s5appdb7DetailsC9tableView_22viewForHeaderInSectionSo6UIViewCSgSo07UITableD0C_SitF + 578
    12  appdb                               0x00000001035cca8d $s5appdb7DetailsC9tableView_22viewForHeaderInSectionSo6UIViewCSgSo07UITableD0C_SitFTo + 77
    13  UIKitCore                           0x00007fff47354cc2 -[UITableView _delegateViewForHeaderInSection:] + 64
    14  UIKitCore                           0x00007fff4735fe22 __96-[UITableView _sectionHeaderView:withFrame:forSection:floating:reuseViewIfPossible:willDisplay:]_block_invoke.3056 + 67
    15  UIKitCore                           0x00007fff47628a26 +[UIView(Animation) performWithoutAnimation:] + 84
    16  UIKitCore                           0x00007fff4735f90f -[UITableView _sectionHeaderView:withFrame:forSection:floating:reuseViewIfPossible:willDisplay:] + 416
    17  UIKitCore                           0x00007fff47360576 -[UITableView _sectionHeaderViewWithFrame:forSection:floating:reuseViewIfPossible:willDisplay:] + 65
    18  UIKitCore                           0x00007fff47328dc4 -[UITableView _updateVisibleHeadersAndFootersNow:] + 3150
    19  UIKitCore                           0x00007fff4732a511 -[UITableView _updateVisibleCellsNow:] + 3412
    20  UIKitCore                           0x00007fff4734a318 -[UITableView layoutSubviews] + 163
    21  UIKitCore                           0x00007fff47636722 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478
    22  QuartzCore                          0x00007fff2b030ef9 -[CALayer layoutSublayers] + 255
    23  QuartzCore                          0x00007fff2b0358ff _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 517
    24  QuartzCore                          0x00007fff2b041fe4 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 80
    25  QuartzCore                          0x00007fff2af8a4a8 _ZN2CA7Context18commit_transactionEPNS_11TransactionEd + 324
    26  QuartzCore                          0x00007fff2afbfab3 _ZN2CA11Transaction6commitEv + 643
    27  QuartzCore                          0x00007fff2afc041a _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 76
    28  CoreFoundation                      0x00007fff23afaeb7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    29  CoreFoundation                      0x00007fff23af594e __CFRunLoopDoObservers + 430
    30  CoreFoundation                      0x00007fff23af5fca __CFRunLoopRun + 1514
    31  CoreFoundation                      0x00007fff23af56b6 CFRunLoopRunSpecific + 438
    32  GraphicsServices                    0x00007fff3815cbb0 GSEventRunModal + 65
    33  UIKitCore                           0x00007fff47162a67 UIApplicationMain + 1621
    34  appdb                               0x00000001033c851b main + 75
    35  libdyld.dylib                       0x00007fff5123bcf5 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
wxxsw commented 5 years ago

It seems that the wrong attributes dictionary parameter type is used. Can you show me your code?

n3d1117 commented 5 years ago

I'm using it like this:

segment = UISegmentedControl(items: ...)
segment.theme_tintColor = ...
segment.theme_setTitleTextAttributes([ [.foregroundColor: UIColor.purple] ], forState: .normal)
contentView.addSubview(segment)

If I use the original method everything works fine:

segment.setTitleTextAttributes([AttributedStringKey.foregroundColor: UIColor.red], for: .normal)

Not sure what's going on. I can't seem to reproduce the crash on a sample project. I will look further into it