作为子视图添加到 UIView 后按钮不可单击

发布于 2024-12-12 02:32:01 字数 861 浏览 0 评论 0原文

我创建了一个名为 UICustomButton 的类,它是 UIView 的子类。我将 UIButton 添加到 UIView 作为 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;
}

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

在此处输入图像描述

编辑:要添加,我在单元格中使用此自定义按钮类实例:

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

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

那一片橙海, 2024-12-19 02:32:01

检查 UICustomButton 对象的框架,以及 self.customButton 是否超出其 superView 范围。

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

香草可樂 2024-12-19 02:32:01

确保子视图的框架位于其父视图的框架内。我遇到这个问题两次,两次子视图的框架都不正确。

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.

长发绾君心 2024-12-19 02:32:01

您应该检查所有属性 userInteractionEnabled 是否已打开:

  1. 检查显示具有此视图的单元格的 UITableView 的属性
  2. 检查 UITableViewCell 的属性,其中您将此视图添加为子视图
  3. 检查 UICustomButton 的属性
  4. 检查 -(id)initWithButtonType:(NSString *)typecustomButton 的属性

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
£噩梦荏苒 2024-12-19 02:32:01

我的按钮不起作用,因为我彼此有两个视图。正如您所看到的,第一个视图的宽度和高度均为 0 像素。我已将此视图设置为与 view2 相同的大小,然后我的按钮可以单击。

    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];

我希望我已经帮助别人了:)

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

梦里的微风 2024-12-19 02:32:01

您的外部视图的高度可能为 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.

逆夏时光 2024-12-19 02:32:01

尝试将其放在 if 语句之后:

self.isTouchEnabled = YES;

try putting this after the if statement:

self.isTouchEnabled = YES;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文