Ios UIButton没有出现
我是一名试图学习iOS开发的python/java程序员。我在一个开源游戏中工作(出于学习目的,可以在这里找到代码:)。我试图以编程方式在重新启动页面上添加一个按钮,如果用户输掉了游戏,则在该页面上单击该按钮。这是我的代码,它没有给我任何错误,但是按钮没有出现。注意,我已经注释掉了开源中提供的代码Ios UIButton没有出现,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我是一名试图学习iOS开发的python/java程序员。我在一个开源游戏中工作(出于学习目的,可以在这里找到代码:)。我试图以编程方式在重新启动页面上添加一个按钮,如果用户输掉了游戏,则在该页面上单击该按钮。这是我的代码,它没有给我任何错误,但是按钮没有出现。注意,我已经注释掉了开源中提供的代码 #import "NewGameScene.h" #import "GameScene.h" @implementation NewGameScene { SKSpriteNode *_butt
#import "NewGameScene.h"
#import "GameScene.h"
@implementation NewGameScene {
SKSpriteNode *_button;
UIButton *myButton;
}
- (id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
[self setBackgroundColor:[SKColor colorWithRed:.61 green:.74 blue:.86 alpha:1]];
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown];
[myButton setTitle:@"Start!" forState:UIControlStateNormal];
myButton.frame = CGRectMake(128, 32, self.size.width/2, self.size.height/10);
[self.view addSubview:myButton];
/*
_button = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithWhite:1 alpha:1] size:CGSizeMake(128, 32)];
[_button setPosition:CGPointMake(self.size.width/2, self.size.height/10)];
[self addChild:_button];
*/
}
return self;
}
-(void) myMethod{
SKTransition *transition = [SKTransition doorsCloseHorizontalWithDuration:.4];
GameScene *game = [[GameScene alloc] initWithSize:self.size];
[self.scene.view presentScene:game transition:transition];
}
/*
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKTransition *transition = [SKTransition doorsCloseHorizontalWithDuration:.4];
GameScene *game = [[GameScene alloc] initWithSize:self.size];
[self.scene.view presentScene:game transition:transition];
}
*/
@end
试试这个
myButton = [UIButton alloc]initWithFrame:CGRectMake(128, 32, 100, 100);
[myButton addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown];
[myButton setTitle:@"Start!" forState:UIControlStateNormal];
[self.view addSubview:myButton];
查看它是否出现在您的视图中。尝试记录self.size以查看它是否真的有值<代码>NSLog(“%d”,自身大小)代码>某些内容不会出现的原因:1)它的大小为零2)它的坐标显示在其父视图的可见区域之外3)它的颜色是透明的4)它有一种颜色,但其alpha设置为0 5)它是隐藏的6)另一个视图被添加到它上面7)你的self.view不知何故为零,因此addSubview什么都不做。在您的情况下,检查1)和2)最有可能。