将子视图添加到UIView后,按钮无法单击

Zhe*_*hen 3 objective-c uibutton uiview ios

我创建了一个名为的类UICustomButton,它是一个子类UIView.我添加了一个UIButtonUIView作为subview在下面我的代码如下所示:

-(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;
}
Run Code Online (Sandbox Code Playgroud)

我面临的问题是,虽然按钮出现,但它们不可点击.我添加按钮的方式有什么问题UIView吗?

在此输入图像描述

编辑:要添加,我将这个自定义按钮类实例用于单元格:

UICustomButton *customButton = [[UICustomButton alloc]initWithFrame:someFrame];
[cell.contentView addSubView:customButton];
Run Code Online (Sandbox Code Playgroud)

小智 8

检查UICustomButton对象的框架,如果self.customButton超出它的框架superView.

  • 你能告诉我如何检查UIButton的框架以及按钮是否超出了它的superView? (7认同)