Take the 2-minute tour ×

i created a custom UIButton inside a scrollview. But the picture does not show, any idea what i am doing wrong ?

   UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                [button setBackgroundImage:[UIImage imageNamed:@"anzahl.png"] forState:UIControlStateNormal];
                [button setTitle:@"-" forState:UIControlStateNormal];
                button.frame = CGRectMake(x, y, viewWidth, viewHeight);//width and height should be same value
                button.clipsToBounds = YES;
                button.layer.cornerRadius = 20;//half of the width
                button.layer.borderColor=[UIColor blueColor].CGColor;
                [button setAlpha:1.0];
                [scroller1 addSubview:button];
                [button release];
share|improve this question
are you passing the correct frames?? –  Hisenberg Oct 4 '11 at 15:49
I'm a little new to iOS myself, but should you be releasing the button if it wasn't alloc/init-ed? –  Gregir Oct 4 '11 at 15:50
great :)) That was the problem, when i release it it is not shown. I first created it with alloc with frame ... later replaced it. Thanks problem solved –  Ferdi Oct 4 '11 at 16:00
add comment

1 Answer

up vote 1 down vote accepted

You don't need to release the button, as it's not been allocated. You've called a class method on UIButton, so no release is needed.

I'd also suggest you first verify that the image is loading correctly so test

UIImage* backgroundImage = [UIImage imageNamed:@"anzahl.png"];
[button setBackgroundImage:backgroundImage forState:UIControlStateNormal]; //breakpoint and ensure that backgroundImage is not nil (0x0)

Or you could have your frame co-ordinates set incorrectly so that the button is drawing outside of the visible screen area, so the next step is ensure that your viewWidth & viewHeight are the correct size and not 0. Also make sure that your x & y co-ordiantes are in the currently visible area of the screen and not outside the visible screen bounds (remember that the x,y position will be relative to the super view they are placed in (in this case scroller1)).

share|improve this answer
yep releasing was my problem thank you :) –  Ferdi Oct 4 '11 at 16:03
add comment

Your Answer


