Tony Hoang
Tony Hoang

Reputation: 1

UIButton in UIView as a subview not Tapping(No Action, No Colour Change)

I have a UIView that is a subview of a UINavigationController Ex.

- (void) ViewDidload{
    UIView *theSubView = [[UIView alloc] init];

    UIButton *button = [UIButton . . . .]
    . . . .
    [theSubView addSubView:button];
    [self.view addSubview:theSubView];
}

All the labels and buttons i put inside "theSubView" SHOW UP but they dont response to any touch.

The UIButton WORKS if it in NOT a subview of "theSubView" but when it is a subview of self it works.

So my question is how do i make the Button inside "TheSubView" (UIView) work?? It doesn't light up or anything.

Upvotes: 0

Views: 2631

Answers (1)

Johannes Lumpe
Johannes Lumpe

Reputation: 1762

If I'm correct then you problem is actually that you use

[[UIView alloc] init]

instead of the designated initializer

initWithFrame:(CGRect)frame

You are creating a view who's bounds are 0,0. You should create it using the designated initializer and do something like this

/*
 * note that the values are just sample values.
 * the view's origin is  0,0 and it's width 320, it's height 480
*/
UIView *theSubView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480];

If you set

theSubView.clipsToBounds = YES

the result should be that you won't see your button at all, because the view's size is 0,0.

A UIView can only respond to touches inside his own bounds, this is why your button doesn't respond to any touches.

Upvotes: 10

Related Questions