views:

271

answers:

2

Hi everybody I create this button like this:

_myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

    DLBadgeLayerDelegate *layerDelegate = [[DLBadgeLayerDelegate alloc] init];

    _myButton.frame = CGRectMake(0.0 , 0.0, 100.0, 100.0);
    [_myButton addTarget:self action:@selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    _myButton.layer.backgroundColor = [[UIColor redColor] CGColor];

    _myButton.center = CGPointMake(10.0, 10.0);
    [self addSubview:_myButton];
    [_myButton release];

The button appears on the screen but if I tap on it nothing happens. If I just create a Rounded rect button instead with no custom drawing on its layer or if I just leave the layer line of code out all works well. Do you have any Idea why do I get this behaviour as I would like to do some custom drawing in the button's layer?

+1  A: 

Events are not sent to a control that is (1) Hidden, (2) alpha < 0.1, (3) userInteraction=NO, etc. Since you have no button image, no button title, no background color, no background image, maybe it is being treated as transparent (like alpha < 0.1) and is not getting events. Try adding an image, title, or background color (in the UIView, not the layer) to see if that's the problem.

EDIT: Try putting the background color in the view's backgroundColor instead of in the layer's backgroundColor:

_myButton.backgroundColor = [UIColor redColor];
progrmr
A: 

If I leave this line out:

_myButton.layer.backgroundColor = [[UIColor redColor] CGColor];

the event is triggered correctly and I still have no title, alpha, etc. I don't think that is the problem or explication, but I will try that way too.

Horatiu Paraschiv