使用 `valueForKey` 访问 UIBarButtonItem 中的视图,违反私有 API? [英] Using `valueForKey` to access view in UIBarButtonItem, private API violation?

查看:21
本文介绍了使用 `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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆