Using `valueForKey` to access view in UIBarButtonItem, private API violation?(使用 `valueForKey` 访问 UIBarButtonItem 中的视图,违反私有 API?)
问题描述
由于UIBarButtonItem
不是UIView
的子类,因此不可能获得像它的frame
这样的正常特征.
Since UIBarButtonItem
doesn't subclass UIView
, it's impossible to get at the normal characteristics like its frame
.
一种方法是 [barButtonItem valueForKey:@"view"]
这非常有效,并且允许您将 GestureRecognizer(例如)添加到底层 UIView
.
This works perfectly, and allows you to add a GestureRecognizer (for instance) to the underlying UIView
.
但是,这是一个私有的 UIKit
API 违规吗?
However, is this a private UIKit
API violation?
推荐答案
这在验证后立即拒绝方面不是私有的,但它的私有性足以被认为是脆弱的(也就是说,新的 iOS 版本可能会破坏您现有的应用程序使用代码的应用商店).
This is not private in terms of immediate rejection upon validation, but it's private enough to be considered fragile (that is, new iOS version can break your existing app in the app store that's using the code).
我可以说,类似的代码(通过 KVC 获取 UIToolbar 的 backgroundView ivar)已经通过了应用商店的验证,并且正在生产中使用.
I can say, that a similar code (fetching backgroundView ivar of UIToolbar via KVC) has passed app store validation and is being used in production.
如果可能发生坏事,您必须将方法包装在 @try { ... } @catch
中,以便在较新的 iOS 版本中拦截可能失败的 KVC.
In case of possible bad things, you must wrap the method in @try { ... } @catch
, so that you intercept KVC possibly failing in newer iOS release.
这篇关于使用 `valueForKey` 访问 UIBarButtonItem 中的视图,违反私有 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 `valueForKey` 访问 UIBarButtonItem 中的视图,违反私有 API?


- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01