【问题标题】:How to make clickable UIButton over UIImageView?如何在 UIImageView 上制作可点击的 UIButton?
【发布时间】:2011-12-16 12:32:32
【问题描述】:

我想创建一个 UIButton 来显示在 UIImageView 上。我的代码sn-p如下图:

imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image

imageView.userInteractionEnabled = YES; //to enable touch interaction with image



UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"ClickMe" forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont systemFontOfSize:16]];


 btn.frame = CGRectMake(340, 550, 70, 50);

[btn addTarget:self action:@selector(onClickHotSpotButton)   forControlEvents:UIControlEventTouchUpInside]; //it wasnt working


[imageView addSubview:btn];
[self addSubview:imageView]; 

我的应用程序在我想要的部分图像上准确显示按钮,但它不响应单击图像。相反,它确实检测到单击,因为我在其检测中记录输出。但我希望这个按钮能够点击选择并执行一些功能。

提前致谢。

瓦希布

【问题讨论】:

    标签: iphone ios ipad uiimageview uibutton


    【解决方案1】:

    尝试设置启用按钮的用户交互。

    [btn setEnabled:YES];
    

    因为 UIImageView 将默认禁用按钮的用户交互。

    否则你可以试试这个。只需更改最后两行。

    替换:

    [imageView addSubview:btn];
    [self addSubview:imageView]; 
    

    用这个:

    [self addSubview:imageView];
    [self addSubview:btn];
    

    确保您首先添加图像视图,然后添加按钮,正如我所提到的。否则按钮将 b 放置在 imageview 后面并且看起来是隐藏的。

    【讨论】:

      【解决方案2】:
      1. 您必须将定义按钮添加为自定义。它将为您工作。

        UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];

      【讨论】:

        【解决方案3】:

        你可以只用一个按钮说:

        [UIButton setImage:[imageNamed:@"image.png"]];
        

        【讨论】:

        • 你的解决方案似乎是最优雅的
        【解决方案4】:

        您需要将按钮添加为 a 的子类

        滚动视图

        不是

        图像视图

        .. 如果按钮是 uiimageview 的子类,它将无法点击。 为每个按钮分配一个特殊标签以识别单击的按钮

        希望对你有帮助

        【讨论】:

          【解决方案5】:

          我试过了,它对我有用.......

          UIImageView * imageView = [[UIImageView alloc]init];
          [imageView setImage:[UIImage imageNamed:@"image.jpeg"]];
          [imageView setFrame:CGRectMake(10,10,300,300)];
          [imageView setContentMode:UIViewContentModeScaleToFill];
          [imageView setUserInteractionEnabled:TRUE];
          
          UIButton * ButtonOnImageView = [[UIButton alloc]init];
          [ButtonOnImageView setFrame:CGRectMake(135,120,60,30)];
          [ButtonOnImageView setImage:[UIImage imageNamed:@"image.jpeg"] forState:UIControlStateHighlighted];
           [ButtonOnImageView setImage:[UIImage imageNamed:@"image2.jpeg"] forState:UIControlStateNormal];
          [ButtonOnImageView addTarget:self action:@selector(OnClickOfTheButton) forControlEvents:UIControlEventTouchUpInside];
          
           [imageView addSubview:ButtonOnImageView];
           [self.view addSubview:imageView];
          

          【讨论】:

          • 如果你在按钮上设置图片,为什么还要使用 UIImageView?
          【解决方案6】:

          我可以在 UIImageVIew 上显示 UIButton,这是更新代码。我已经在底部提到了最近的问题。

          imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image
          
          //imageView.tag = i; // tag our images for later use when we place them in serial form
          [imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact
          imageView.userInteractionEnabled = YES; //to enable touch interaction with image
          
          [self addSubview:imageView];
          
          UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button
          
          btn.frame = CGRectMake(215, 550, 100, 100);
          [btn addTarget:self action:@selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
          
          **[self addSubview:btn]; //Buttons are clickable but shows button on all images**
          
          //[imageView addSubview:btn]; //Buttons are not clickable and are shown on all images
          

          我现在有两个选择仪式。我是否让我的按钮成为 ImageView 的子视图或作为 ImageView 的父级的 ScrollView。如果我制作 Scrollview 的按钮子视图,例如 [self addSubView:btn];它显示在正确的位置并且是可点击的,但问题是它是在队列中的所有图像上创建的,因为我将它设为父视图的子视图,即滚动视图。否则,如果我将其设为子视图的子视图,即 ImageView,它对每个图像都是唯一的,但仍显示在所有图像上且不可点击:/

          any1 可以指导我如何使其可点击并保持动态按钮和图像之间的链接唯一,以便我在队列中每个图像的不同位置有不同的按钮。

          【讨论】:

            【解决方案7】:

            我在我的一个应用中做了类似的事情。我创建了一个包含一系列图像的 xib 文件,每个图像的顶部都有一个按钮。我将它们连接到类文件中的相应插座。然后我计算出在 touchesBegan 中点击了哪个:

            if(CGRectContainsPoint(btnFirstButton.frame, touchLocation)){
                //do something useful
            }else if(CGRectContainsPoint(btnSecondButton.frame, touchLocation)){
                //do something useful
            }
            

            希望有帮助 - 对我有用 :-)

            【讨论】:

              【解决方案8】:

              如果是可点击的图片,我更喜欢图片而不是按钮。

              UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
              [sampleButton setFrame:CGRectMake(kLeftMargin, 10, self.view.bounds.size.width - kLeftMargin -      kRightMargin, 52)];
              [sampleButton setTitle:@"Button Title" forState:UIControlStateNormal];
              [sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
              [sampleButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
              [sampleButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
              [self.view addSubview:sampleButton];
              

              【讨论】:

              • 感谢您的回复。它是否是图像按钮对我来说并不重要。我想要的只是让它可点击。我现在有两个选择仪式。我是否让我的按钮成为 ImageView 的子视图或作为 ImageView 的父级的 ScrollView。如果我制作 Scrollview 的按钮子视图,例如 [self addSubView:btn];它显示在正确的位置并且是可点击的,但问题是它是在队列中的所有图像上创建的,因为我将它设为父视图的子视图,即滚动视图。否则,如果我将其设为子视图的子视图,即 ImageView 对于每个图像都是唯一的,则它不可点击:/
              猜你喜欢
              • 2015-09-14
              • 1970-01-01
              • 1970-01-01
              • 2012-10-10
              • 2011-09-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多