drawRect在子视图? [英] drawRect over subviews?

查看:120
本文介绍了drawRect在子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含一堆子视图的容器视图 - 一个集合视图,一个自定义工具栏和一些零碎的部分。

I created a container view that holds a bunch of child views - a collection view, a custom toolbar and some bits and pieces.

设计在顶部,左侧和右侧有边框,但不在底部,所以我覆盖了drawRect以包含边框。

The design has a border on the top, left and right sides, but not the bottom, so I overrode drawRect to include border.

当我添加工具栏时,我注意到它出现在边框的顶部。 (出于某种原因,我最初认为它不会,但当然它确实如此!)。

When I added the toolbar I noticed that it appears over the top of the border. (For some reason I initially thought it wouldn't but of course it does!).

无论如何,我可以告诉drawRect绘制我的子视图的顶部吗?

Is there anyway I can tell drawRect to draw over the top of my subviews?

当然还有很多其他方法可以解决我的问题(例如调整工具栏的框架)但是我问这个问题是为了深入了解绘图的工作原理合成和视图层次结构。

Of course there's loads of other ways to solve my problem (adjust the toolbar's frame for example) however I'm asking this question in order to get a deep understanding of how drawing works in relation to compositing and the view hierarchy.

推荐答案

绘图发生在UIView的所有子视图下面。将其视为基础 - 视图的实际部分 - 然后将每个子视图添加到视图的顶部。要使子视图上方的绘图与希望子视图显示在视图下方,同时仍然是子视图相同。也许这个比喻更清楚为什么它必须始终在底层。它还引导您逻辑上找到解决方案:

Drawing happens beneath all subviews of a UIView. Think of it as being on the very base - an actual part of your view - and then each subview is added on top of your view. To make the drawing above the subviews is the same as wanting for the subviews to appear under the view, while still being subviews. Perhaps that analogy makes it clearer why it must always be on the bottom. And it also leads you logically to the solution:

要使绘图显示在子视图上方,只需创建一个新的UIView子类以将绘图代码放在其中,然后放置高于所有其他子视图。

To get the drawing to appear above subviews, simply create a new UIView subclass to place the drawing code inside, and place this above all other subviews.

覆盖你的UIView的 addSubview:实现也是一个好主意,以确保您的子视图始终保持最佳状态。

It might also be a good idea to override your UIView's addSubview: implementation, to ensure your subview always remains on top.

这篇关于drawRect在子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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