drawRect on top of subviews
Asked Answered
I

2

40

I have subclassed UIView and added a drawRect method to it. Then I define a view using this custom class and add subviews to it.

The problem is that drawRect seem to draw stuff under the subviews (hence not visible).

I want stuff that drawRect draws appear above the subviews of my custom UIView.

Is this possible?

Intramolecular answered 8/10, 2011 at 6:9 Comment(2)
Please give an example (possibly images?) of what you are trying to achieve. It sounds like you want to draw the subviews content first and the the "background" on top of that, but I can't be sure.Roseroseann
Great question, thanks for keeping it concise.Mettlesome
R
30

A subview will always be drawn on "top" of its superview.

Depending on your specific requirements, you may need to have a plain UIView as the background/container view, with your existing subviews in there, and then your custom view as another subview added to the very top, so it has the highest Z-order.

This would work, for example, if your custom view was a grid overlay that should be on top of everything else. The custom view would have to be non-opaque with a clear background colour.

Roseroseann answered 8/10, 2011 at 8:58 Comment(0)
A
10

No, The subviews always appear above their container. Nothing stops you from making the subviews (partially) transparent, of course.

If you want to draw above, place another transparent subview as the topmost subview into the container and draw in that one. If you disable user interaction on it, it won't interfere with your touch handling.

Amoroso answered 8/10, 2011 at 8:53 Comment(1)
Also worth noting that this forced refactoring (to a new UIView or views) will help readability, in most cases. +1Mettlesome

© 2022 - 2024 — McMap. All rights reserved.