Zhen
Zhen

Reputation: 12431

Button not clickable after adding as a subview to a UIView

I have created a class called UICustomButton, which is a subclass of UIView. I added a UIButton to UIView as a subview as shown in my code below:

-(id)initWithButtonType:(NSString *)type
{
    self = [super init];
    if (self) 
    {
        self.customButton = [self setupButtonWithTitle:type andFrame:frame];
        [self addSubview:self.customButton];
        self.customButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];

    }
    return self;
}

The problem I am facing is that although the button appears, they are not clickable. Is there something wrong with the way I am adding the buttons to the UIView?

enter image description here

EDIT: To add on, I am using this custom button class instance to a cell:

UICustomButton *customButton = [[UICustomButton alloc]initWithFrame:someFrame];
[cell.contentView addSubView:customButton];

Upvotes: 4

Views: 12949

Answers (6)

Chrissi
Chrissi

Reputation: 1218

Your outer view probably has a height of 0. Add constraints that makes it the same height as (or higher than) the subviews, or create it with a frame that is large enough.

Upvotes: 0

user3125062
user3125062

Reputation: 21

Make sure that the frame of the subview is within the frame of its superview. I encountered the issue twice and both times the frame of the subview was incorrect.

Upvotes: 2

Gerrit Post
Gerrit Post

Reputation: 1267

My button was not working because i had two views in each other. The first view, as you can see, has a width and heigth of 0 pixels. I've made this view the same size as view2 and then my button was clickable.

    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(325, 346, 0, 0)];
    view1.alpha = 0.90;

    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 450, 150)];
    [view2.layer setCornerRadius:10.0f];
    view2.backgroundColor = [UIColor whiteColor];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn addTarget:self action:@selector(addClosedToCollection) forControlEvents:UIControlEventTouchUpInside];
    [btn setFrame:CGRectMake(30, 30, 300, 32)];
    [btn setTitle:@"i'm a button" forState:UIControlStateNormal];

    [view2 addSubview:btn];
    [view1 addSubview:view2];

i hope i've helped somebody out :)

Upvotes: 0

Nekto
Nekto

Reputation: 17877

You should check if all properties userInteractionEnabled are on:

  1. Check that property for UITableView where your cells with this view are displayed
  2. Check that property for UITableViewCell where you add this view as subview
  3. Check that property for UICustomButton
  4. Check that property for customButton in -(id)initWithButtonType:(NSString *)type

Upvotes: 0

wcrane
wcrane

Reputation: 1195

check the frame of your UICustomButton object, and if self.customButton is out of its superView.

Upvotes: 10

Gabriel
Gabriel

Reputation: 3045

try putting this after the if statement:

self.isTouchEnabled = YES;

Upvotes: -1

Related Questions