1.UITextField 的私有属性 _placeholderLabel 被禁止访问
历史代码
[self.inputField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
Xcode升级到11.3后,之前正常运行的代码启动后就会发生闪退
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Access to UITextField's _placeholderLabel ivar is prohibited. This is an application bug'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff23c7127e __exceptionPreprocess + 350
1 libobjc.A.dylib 0x00007fff513fbb20 objc_exception_throw + 48
2 CoreFoundation 0x00007fff23c710bc +[NSException raise:format:] + 188
3 UIKitCore 0x00007fff4838e451 -[UITextField valueForKey:] + 84
4 Foundation 0x00007fff257138bb -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 251
......
12 UIKitCore 0x00007fff47a0ef01 -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 83
13 UIKitCore 0x00007fff47a13e5a -[UIViewController loadViewIfRequired] + 1084
14 UIKitCore 0x00007fff479781e4 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 160
15 UIKitCore 0x00007fff479784e8 -[UINavigationController _startTransition:fromViewController:toViewController:] + 144
16 UIKitCore 0x00007fff479793b6 -[UINavigationController _startDeferredTransitionIfNeeded:] + 868
17 UIKitCore 0x00007fff4797a721 -[UINavigationController __viewWillLayoutSubviews] + 150
18 UIKitCore 0x00007fff4795b553 -[UILayoutContainerView layoutSubviews] + 217
19 UIKitCore 0x00007fff485784bd -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478
20 QuartzCore 0x00007fff2b131db1 -[CALayer layoutSublayers] + 255
21 QuartzCore 0x00007fff2b137fa3 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 517
22 QuartzCore 0x00007fff2b1438da _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 80
23 QuartzCore 0x00007fff2b08a848 _ZN2CA7Context18commit_transactionEPNS_11TransactionEd + 324
24 QuartzCore 0x00007fff2b0bfb51 _ZN2CA11Transaction6commitEv + 643
25 UIKitCore 0x00007fff480aa575 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 81
26 CoreFoundation 0x00007fff23bd429c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
27 CoreFoundation 0x00007fff23bd3a08 __CFRunLoopDoBlocks + 312
28 CoreFoundation 0x00007fff23bce894 __CFRunLoopRun + 1284
29 CoreFoundation 0x00007fff23bce066 CFRunLoopRunSpecific + 438
30 GraphicsServices 0x00007fff384c0bb0 GSEventRunModal + 65
31 UIKitCore 0x00007fff48092d4d UIApplicationMain + 1621
32 XtioniPhone 0x000000010f398bd0 main + 112
33 libdyld.dylib 0x00007fff5227ec25 start + 1
34 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
修改方法:UITextField有个attributedPlaceholder的属性,我们可以自定义这个富文本来达到我们需要的结果。iOS 13 通过 KVC 方式修改私有属性,有 Crush 风险,谨慎使用。
NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName : self.placeholderColor}];
_textField.attributedPlaceholder = placeholderString;
网友评论