访问uiviews的子视图可以吗
我看到了许多聪明的想法,可以将视图和控件调整为我们想要的样子,其中一些在 SO 上列出:
- 透明
UISearchBar
:iPad/iPhone uiSearchbar 透明背景 - 不同颜色的
UISegmentedControl
:UISegmentedControl 选定的段颜色
我想知道,这合法吗?每当有人对诸如 [[view subviews] objectAtIndex:0]
之类的东西进行硬编码时,似乎就涉及到一些黑客行为,但似乎到处都是这样做的……有人可以对此发表评论,也许可以给出一些建议吗?观点?按照规则书可以吗?有没有人发布过这样的生产代码并且被苹果接受了?
I have seen many bright ideas to massage views and controls into looking exactly the way we want them, some of them listed here on SO:
- Transparent
UISearchBar
: iPad/iPhone uiSearchbar transparent background - Different colored
UISegmentedControl
: UISegmentedControl selected segment color
I was wondering, is this legal? There seems to be some element of hackery involved whenever someone hardcodes something like [[view subviews] objectAtIndex:0]
, but it seems to be done everywhere...can someone comment on this and perhaps give some perspectives? Is it ok by the rule books? Has anyone released production code like this and has it been accepted by apple?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它作为 API 的一部分公开,因此是合法的。我已经在 App Store 中当前的一个应用程序上完成了此操作,而且我知道其他人也做了同样的事情。
It's exposed as part of the API, therefore it's legal. Ive done it on an application currently in the App Store, and I know others have done the same.
几乎所有好看的应用程序都做到了这一点并且得到了很好的批准。当您决定这样做时,请做好迎接新版本 iOS 的准备,该版本可能会破坏您正在做的事情,并准备好立即发布更新
Just about every good looking app has done this and been approved just fine. When you make the decision to do so, just be prepared for a new version of iOS that may break what you are doing and be ready to release an update immediately
仅仅因为您能够做某事,并不意味着它在所有地方都适合。在某些情况下,访问视图的子视图的能力是必要的,但是对于您不维护的组件,我会担心依赖位于层次结构中特定位置的特定 UIView。无法保证特定子视图在 sdk 的未来版本中始终是第 0 个子视图。
如果这是唯一的方法,那么您可能没有其他选择,但请记住,它是应用程序的脆弱部分,应该针对所有平台上的每个 sdk 版本进行良好的测试。
Just because you are able to do something, doesn't mean it's appropriate in all places. The abilitiy to access the subviews of a view is necessary in some situations, however I would be apprehensive about relying on a particular UIView being at a specific position in the hierarchy for components that you do not maintain. There is no guarantee that a specific subview will always be the zeroth subview in future versions of the sdk.
If this is the only way to do it then you may not have another choice, but remember that it is a fragile part of your app and should be well tested for every sdk release on all platforms.