4

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];
Nekto
  • 17,837
  • 1
  • 55
  • 65
Zhen
  • 12,361
  • 38
  • 122
  • 199
  • What does `setupButtonWithTitle:andFrame:` do? Also, you need to make sure that if the code for `initWithButtonType:` references `self` to add a selector `buttonPressed`, that the `buttonPressed` method is indeed implemented in the same class. –  Oct 26 '11 at 03:43
  • Dup of http://stackoverflow.com/questions/6675233/addsubview-to-uibutton – phatmann Apr 30 '13 at 22:59

6 Answers6

10

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

Nekto
  • 17,837
  • 1
  • 55
  • 65
wcrane
  • 1,195
  • 7
  • 18
  • Yup my superview was not being set up properly. That was causing the problem. Problem solved. Thanks! – Zhen Oct 27 '11 at 10:27
  • 9
    can you please tell me how to check frame of UIButton and if the button is out of its superView? – thandasoru Mar 03 '13 at 16:04
2

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.

0

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 :)

Gerrit Post
  • 1,247
  • 13
  • 25
0

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.

Chrissi
  • 1,188
  • 10
  • 9
0

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
Nekto
  • 17,837
  • 1
  • 55
  • 65
-1

try putting this after the if statement:

self.isTouchEnabled = YES;
Gabriel
  • 3,039
  • 6
  • 34
  • 44