ios - Custom UIButton doesn't show up -
i'm making game , everything's working fine. there seems problem custom button made:
let button = uibutton(type: uibuttontype.custom) 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 run application, button doesn't appear. what's wrong code, there missing?
i integrating ui elements inside sprite-kit scene, , have bit tricky. if give me more information other elements of class, if uiview
or skscene
(skview), or sknode
. when mixing both approaches pain.
but information gave can tell have same problem , solved changing method added ui elements on skscene. before on viewdidload should on viewdidappear.
example (objective-c):
- the icarousel ui element (not skview or sknode).
`
-(void)didmovetoview:(skview *)view { //your code 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 zposition of button , other elements.
make sure image being loaded debugger. extension (.png) might happening prevent so. ( suggested @matt on comments)
i strongly recommend avoid mixing uikit elements sprite-kit elements.
- please give more information if not solve problem ;)
edit: example of how the corresponding uibutton in sprite-kit ( simple way).
(just taken question - setting buttons in skscene )
you use skspritenode button, , when user touches, check if node touched. use skspritenode's name property identify node:
//fire button - (skspritenode *)firebuttonnode { skspritenode *firenode = [skspritenode spritenodewithimagenamed:@"firebutton.png"]; firenode.position = cgpointmake(firebuttonx,firebuttony); firenode.name = @"firebuttonnode";//how node identified later firenode.zposition = 1.0; return firenode; }
add node 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 rain if ([node.name isequaltostring:@"firebuttonnode"]) { //do whatever... } }
Comments
Post a Comment