How to add a subclass as a subview so that background of subview is set in front of superview

霸气de小男生 提交于 2019-12-12 01:32:11

问题


I got 2 classes

@interface PlayScene : UIView

and

@interface GameOverMenu : PlayScene <UITextFieldDelegate>

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];

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

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


回答1:


Use any of the below methods :

  • insertSubview: atIndex:
  • insertSubview: aboveSubview:
  • bringSubviewToFront:

EDIT

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...



来源:https://stackoverflow.com/questions/10303942/how-to-add-a-subclass-as-a-subview-so-that-background-of-subview-is-set-in-front

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!