【问题标题】:can't click UIButton when it's added to UIImageView添加到 UIImageView 时无法单击 UIButton
【发布时间】:2010-08-20 08:19:13
【问题描述】:

伙计们,我想在添加到 UIImageVIew 后单击我的 uiButton,但它不起作用。 这是我的代码:

UIButton *btnDetail = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain];

btnDetail.frame = CGRectMake(0,0,100,100);
[btnDetail addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventAllTouchEvents];

[self.imageView addSubview:btnDetail];

当我将按钮添加到 UIImageVIew 时无法单击,但如果我不将其添加到 UIImageView,它可以正常工作。 请有人帮助我

【问题讨论】:

    标签: iphone uiimageview uibutton


    【解决方案1】:

    注意:默认情况下,UIImageViewUserInteraction 属性设置为 NO。这是我们大多数人犯错的地方。 因此,在向UIImageView 添加任何控件时要检查的主要内容是将其UserInteractionEnabled 属性设置为YES

    [self.imageView setUserInteractionEnabled:YES];
    

    所以修改你的代码如下:

    UIButton *btnDetail = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain];
    
    btnDetail.frame = CGRectMake(0,0,100,100);
    [btnDetail addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventAllTouchEvents];
    
    [self.imageView addSubview:btnDetail];
    [self.imageView bringSubviewToFront:btnDetail];
    [self.imageView setUserInteractionEnabled:YES];
    

    快乐的编码...

    【讨论】:

    • 我尝试了你的代码,但没有任何反应,我仍然无法点击它。更多信息:在我添加 UIButton 之前,我已经在这个 UiVIewImage 中添加了另一个 UIImage,有影响吗??
    • 请查看新代码。我已经实现并测试了。它现在工作正常.. 实际上 imageview 的用户交互默认情况下是关闭的,因此您无法单击添加到其中的任何内容。所以我已将其使用交互设置为启用。现在工作正常
    • thnx 伙计们,它工作得很好,我不敢相信我的错误只是把 [self.imageView setUserInteractionEnabled:YES];在我添加子视图之前,确保它不起作用,哈哈可笑,顺便说一句,非常感谢你们......
    • 这件事花了我 4-5 天,被这样的问题卡住了
    • userInteractionEnabled = 否。帮我点击一下UIImageView。
    【解决方案2】:

    UIImageView 默认将userInteractionProperty 设置为NO,因此它不处理触摸并且不将它们传播到其子视图。尝试将其设置为 YES:

    self.imageView.userInteractionEnabled = YES;
    

    【讨论】:

    • @Imam Arief W。如果这不起作用,那么还有其他问题。
    【解决方案3】:

    嘿,我用过这段代码。它工作正常

     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(80.0, 210.0, 100.0, 20.0);
    [button addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"ShowView" forState:UIControlStateNormal];
    
    [image2 addSubview:button];
    [image2 bringSubviewToFront:button];
    [image2 setUserInteractionEnabled:YES];
    

    【讨论】:

      【解决方案4】:

      如果有人在 UIImageView 中与 UIButton 有交互问题,请执行下一步。

      1 创建 UIImageView 的属性:

      @property (nonatomic, strong) UIImageView *buttonImageView;
      

      2 比合成它:

      @synthesize buttonImageView = _buttonImageView;
      

      3 创建实现方法:

      - (UIImageView *) buttonImageView {
      
          _buttonImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
          [_buttonImageView setBackgroundColor:[UIColor whiteColor]];
          [_buttonImageView setUserInteractionEnabled:YES];
      
          UIButton *addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
          [addButton setTitle:@"add button" forState:UIControlStateNormal];
          addButton.frame = CGRectMake(0, 0, 160, 50);
          [addButton setUserInteractionEnabled:YES];
          [addButton setEnabled:YES];
          [addButton setAlpha:1];
          [addButton addTarget:self action:@selector(addNewImage) forControlEvents:UIControlEventTouchUpInside];
      
          return _buttonImageView;
      }
      

      4 例如在 viewDidLoad 方法中添加子视图到你的视图中:

      [self.view addSubview:self.buttonImageView];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-06
        • 1970-01-01
        • 1970-01-01
        • 2012-10-10
        • 2018-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多