如何将子类添加为子视图,以便将子视图的背景设置在超级视图的前面 [英] How to add a subclass as a subview so that background of subview is set in front of superview
问题描述
我有2节课
@interface PlayScene : UIView
和
@interface GameOverMenu : PlayScene <UITextFieldDelegate>
在PlayScene中,我创建GameOverMenu的实例
in PlayScene I create an instance of GameOverMenu
GameOverMenu* gorm = [[GameOverMenu alloc]initWithFrame:CGRectMake(0, 0, 320, 520)];
gorm.backgroundColor = [UIColor blackColor];
[self addSubview:gorm];
但是背景不是设置为子视图,而是设置为超级视图,我的意思是背景不会隐藏PlayScene视图的元素,因此按钮和某些图形保留在背景的前面
But background is set not for subview but for superview, I mean background doesn't hide elements of PlayScene view so that buttons and some drawing remain in front of the background
如果问题仍然不清楚,我想使子视图站在黑色(例如)背景下的超级视图前面,覆盖整个超级视图前面的整个屏幕. 好像子视图类仅从UIView继承,而不是从PlayScene继承
If the problem is still not clear, I want to make my subview stand in front of superview with black(for example) background, covering the whole screen in front of superview. As if subview class was inherited just from UIView and not from PlayScene
推荐答案
使用以下任何一种方法:
Use any of the below methods :
-
insertSubview: atIndex:
-
insertSubview: aboveSubview:
-
bringSubviewToFront:
insertSubview: atIndex:
insertSubview: aboveSubview:
bringSubviewToFront:
编辑
如果尝试创建GameOverMenu
的实例,该实例是PlayScene
的子类,然后将该实例作为子视图添加到PlayScene
的视图中,则可能可以尝试创建每个PlayScene
的实例和GameOverMenu
并将这两个作为子视图添加到第三个视图中..也许是ViewControllers视图,或者仅仅是一个视图..根据您的要求...
If you are trying to create an instance of GameOverMenu
which is subclass of PlayScene
and then add this instance as subview on the view of PlayScene
, then probably you can try creating an instance each of PlayScene
and GameOverMenu
and adding these two as subview to a third view.. maybe ViewControllers view or maybe just a view.. based on your requirement...
这篇关于如何将子类添加为子视图,以便将子视图的背景设置在超级视图的前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!