【问题标题】:Button not responding to touch按钮不响应触摸
【发布时间】:2023-03-19 04:54:01
【问题描述】:

在我的应用程序中,我将UIImageview 添加到UIView,并将UIButton 添加到UIImageView。该按钮不响应触摸事件并且不调用动作选择器。为什么?

【问题讨论】:

    标签: iphone uiimageview uibutton


    【解决方案1】:

    因为UIImageViewuserInteractionEnabled 属性默认设置为NO。将其设置为YES

    【讨论】:

    • 顺便说一句,这是 UIKit 中另一个无脑的不一致。这是故意的,但它比好的更令人困惑。
    【解决方案2】:
    UIIMageView *image;
    image.userInteractionEnabled=YES;
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。您可以在帮助中心找到更多关于如何写好答案的信息:stackoverflow.com/help/how-to-answer。祝你好运?
    【解决方案3】:

    首先你添加图像视图然后添加按钮然后按钮像这段代码一样正常工作

     UIView *AddView=[[UIView alloc]initWithFrame:CGRectMake(55, 0, 100, 100)];
     UIImageView *imgview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    
    
                    UIButton *btnRemove = [[UIButton alloc]init];
                    UIImage *btnImage = [UIImage imageNamed:@"close.png"];
                    [btnRemove setImage:btnImage forState:UIControlStateNormal];
                    [btnRemove addTarget:self
                                  action:@selector(btnREmoveAction:)
                        forControlEvents:UIControlEventTouchUpInside];
                    btnRemove.frame = CGRectMake(0,0, 29, 29);
    
                    btnRemove.tag=indexStart;
    
                    [AddView addSubview:imgview]; // add the imageview
                    [AddView addSubview:btnRemove];  // add the button 
    

    【讨论】:

      【解决方案4】:

      确保您已将 IBAction 附加到您的按钮。然后检查它是否启用了用户交互。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-04
        • 1970-01-01
        • 2016-06-23
        • 1970-01-01
        • 2021-10-31
        • 2016-12-14
        相关资源
        最近更新 更多