开发者

How to have the parent UIView Appears ABOVE it's UIImageView sub-view

I have a class that extends UIView, and it has a UIImageView that is added as a subview when the UIView is constructed. I override the Draw method (aka drawRect) and draw on the current CGContext just fine if I don't add the UIImageView or if I set it Opaque to false and BackgroudColor to Clear and leave Image set to null (aka nil). Once I set the BackgroundColor of the UIImageView to a real color, or I set the Image on the UIImageView to something other than null everything I draw is being drawn UNDER the UIImageView.

This seems odd that if if I draw on the parent's context that is should appear above any sub views. However it seems the stack is upside down from what one would think. It seems one would expect that the highest layer is always the parent with all the sub views being added to lower layers in the order they are added. This doesn't seem to be the case.

I tried sending the sub view to front and to back but it didn't change anything.

Basically if my parent view Opaque is false and it's background colo开发者_JS百科r is Clear I would expect any shapes or lines I draw on it's context in its draw method to be shown ON TOP of any subviews it may have.

How can I draw on a parent View and while showing below what is being drawn a subview?

Is there any documentation on Apple's site that explains the Z-order of a Parent View and its sub views?

Examples in C# or Objective-C are fine.


The behavior you are seeing is correct! In UIKit a view draws itself in drawRect and then the framework handles drawing all of its subviews on top of it.

There isn't really a way to change this since Apple has kind of hard coded the z order. But it really does make sense. For instance, if I have a background image and I want to add a person to it I would do [backgroundView addSubview:personView] and I would expect that person view would be drawn on top of the backgroundView.

If you want to draw over your image do your drawing in a different view and add that on top:

[mainView addSubview:myImageView]
[mainView addSubview:myCustomOverlayView]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜