自定义UIButton不会显示

时间:2022-07-14 21:08:38

I'm making a game and everything's working fine. But there seems to be a problem with this custom button I made:

我正在制作游戏,一切正常。但是我的这个自定义按钮似乎有问题:

let button = UIButton(type: UIButtonType.Custom) as UIButton
let image = UIImage(named: "bluePlayButton")
button.setImage(image, forState: UIControlState.Normal)
button.frame = CGRectMake(self.size.width/2, self.size.height * 0.15, 300, 200)
button.layer.zPosition = 1

self.view?.addSubview(button)

When I run the application, the button just doesn't appear. What's wrong with my code, is there something missing?

当我运行应用程序时,按钮就不会出现。我的代码出了什么问题,有什么遗漏?

1 个解决方案

#1


1  

I am currently integrating UI elements inside a sprite-kit scene, and i have to say that is a bit tricky. If you could give me more information about the other elements of the class, if is it a UIView or a SKScene (SKView), or a SKNode. When mixing both approaches this really is a pain.

我目前正在sprite-kit场景中集成UI元素,我不得不说这有点棘手。如果您可以提供有关该类其他元素的更多信息,如果它是UIView或SKScene(SKView),还是SKNode。当混合两种方法时,这真的很痛苦。

But with the information you gave I can tell you that I have the same problem and I solved changing the method where a I added the UI elements on the SKScene. Before it was on the viewDidLoad but I should do it on the viewDidAppear.

但是根据您提供的信息,我可以告诉您我遇到了同样的问题,并且我解决了更改我在SKScene上添加UI元素的方法的问题。之前它在viewDidLoad上,但我应该在viewDidAppear上进行。

EXAMPLE (Objective-C):

  • The iCarousel is a UI element (not SKView or SKNode).
  • iCarousel是一个UI元素(不是SKView或SKNode)。

`

-(void)didMoveToView:(SKView *)view {
//Your code but in Objective-C
UIButton * button  = UIButton
UIButton * button  = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage * image = [UIImage imageNamed:@"bluePlayButton"];
[button setImage:image forState:UIControlStateNormal];
[button setFrame:CGRectMake(self.size.width/2, self.size.height * 0.15, 300, 200)];
button.layer.zPosition=1;
[self.view addSubView:button];
}

`

  • Also, double check the zPosition of the button and the other elements.

    另外,仔细检查按钮的zPosition和其他元素。

  • Make really sure that the image is being loaded properly with the debugger. Even with the extension (.png) something might be happening to prevent so. ( as suggested by @matt on the comments)

    确保使用调试器正确加载图像。即使使用扩展名(.png),也可能会发生一些事情以防止这种情况发生。 (正如@matt对评论的建议)

  • I STRONGLY recommend to avoid mixing UIKit elements with Sprite-Kit elements.

    我强烈建议避免将UIKit元素与Sprite-Kit元素混合使用。

  • Please give some more information if this does not solve your problem ;)
  • 如果这不能解决您的问题,请提供更多信息;)

EDIT: Another example of how to do the the corresponding to UIButton in Sprite-Kit ( the most simple way).

编辑:如何在Sprite-Kit中执行与UIButton相对应的另一个示例(最简单的方法)。

(Just taken from this question - Setting up buttons in SKScene )

(刚从这个问题中解脱出来 - 在SKScene中设置按钮)

You could use a SKSpriteNode as your button, and then when the user touches, check if that was the node touched. Use the SKSpriteNode's name property to identify the node:

您可以使用SKSpriteNode作为按钮,然后当用户触摸时,检查是否触摸了节点。使用SKSpriteNode的name属性来标识节点:

//fire button
- (SKSpriteNode *)fireButtonNode
{
    SKSpriteNode *fireNode = [SKSpriteNode spriteNodeWithImageNamed:@"fireButton.png"];
    fireNode.position = CGPointMake(fireButtonX,fireButtonY);
    fireNode.name = @"fireButtonNode";//how the node is identified later
    fireNode.zPosition = 1.0;
    return fireNode;
}

Add node to your scene:

将节点添加到场景中:

[self addChild: [self fireButtonNode]];

Handle touches:

//handle touch events
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    //if fire button touched, bring the rain
    if ([node.name isEqualToString:@"fireButtonNode"]) {
         //do whatever...
    }
}

#1


1  

I am currently integrating UI elements inside a sprite-kit scene, and i have to say that is a bit tricky. If you could give me more information about the other elements of the class, if is it a UIView or a SKScene (SKView), or a SKNode. When mixing both approaches this really is a pain.

我目前正在sprite-kit场景中集成UI元素,我不得不说这有点棘手。如果您可以提供有关该类其他元素的更多信息,如果它是UIView或SKScene(SKView),还是SKNode。当混合两种方法时,这真的很痛苦。

But with the information you gave I can tell you that I have the same problem and I solved changing the method where a I added the UI elements on the SKScene. Before it was on the viewDidLoad but I should do it on the viewDidAppear.

但是根据您提供的信息,我可以告诉您我遇到了同样的问题,并且我解决了更改我在SKScene上添加UI元素的方法的问题。之前它在viewDidLoad上,但我应该在viewDidAppear上进行。

EXAMPLE (Objective-C):

  • The iCarousel is a UI element (not SKView or SKNode).
  • iCarousel是一个UI元素(不是SKView或SKNode)。

`

-(void)didMoveToView:(SKView *)view {
//Your code but in Objective-C
UIButton * button  = UIButton
UIButton * button  = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage * image = [UIImage imageNamed:@"bluePlayButton"];
[button setImage:image forState:UIControlStateNormal];
[button setFrame:CGRectMake(self.size.width/2, self.size.height * 0.15, 300, 200)];
button.layer.zPosition=1;
[self.view addSubView:button];
}

`

  • Also, double check the zPosition of the button and the other elements.

    另外,仔细检查按钮的zPosition和其他元素。

  • Make really sure that the image is being loaded properly with the debugger. Even with the extension (.png) something might be happening to prevent so. ( as suggested by @matt on the comments)

    确保使用调试器正确加载图像。即使使用扩展名(.png),也可能会发生一些事情以防止这种情况发生。 (正如@matt对评论的建议)

  • I STRONGLY recommend to avoid mixing UIKit elements with Sprite-Kit elements.

    我强烈建议避免将UIKit元素与Sprite-Kit元素混合使用。

  • Please give some more information if this does not solve your problem ;)
  • 如果这不能解决您的问题,请提供更多信息;)

EDIT: Another example of how to do the the corresponding to UIButton in Sprite-Kit ( the most simple way).

编辑:如何在Sprite-Kit中执行与UIButton相对应的另一个示例(最简单的方法)。

(Just taken from this question - Setting up buttons in SKScene )

(刚从这个问题中解脱出来 - 在SKScene中设置按钮)

You could use a SKSpriteNode as your button, and then when the user touches, check if that was the node touched. Use the SKSpriteNode's name property to identify the node:

您可以使用SKSpriteNode作为按钮,然后当用户触摸时,检查是否触摸了节点。使用SKSpriteNode的name属性来标识节点:

//fire button
- (SKSpriteNode *)fireButtonNode
{
    SKSpriteNode *fireNode = [SKSpriteNode spriteNodeWithImageNamed:@"fireButton.png"];
    fireNode.position = CGPointMake(fireButtonX,fireButtonY);
    fireNode.name = @"fireButtonNode";//how the node is identified later
    fireNode.zPosition = 1.0;
    return fireNode;
}

Add node to your scene:

将节点添加到场景中:

[self addChild: [self fireButtonNode]];

Handle touches:

//handle touch events
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    //if fire button touched, bring the rain
    if ([node.name isEqualToString:@"fireButtonNode"]) {
         //do whatever...
    }
}