我可以在ImageView上放一个按钮吗?

时间:2012-11-02 08:24:24

标签: iphone objective-c ios ipad

如何将Round Rect按钮放在imageView上,

我的imageView:self.view addSubview:myImage

更新

谢谢大家! 这些对我有帮助。

5 个答案:

答案 0 :(得分:2)

UIButton *myButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame=CGRectMake(10,20,30,40); // set the size of the button here
[myImage addSubview:myButton];
[self.view addSubView:myImage]; 

答案 1 :(得分:2)

 UIImageView *contentView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,200,200)];
[contentView setImage:[UIImage imageNamed:@"helloworld.png"]];

UIButton *b=[[UIButton alloc] init];    
b.center=CGPointMake(160, 240);
[b setImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
[b addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchUpInside];
[contentView addSubview:b];

// Provide support for auto-rotation and resizing
contentView.autoresizesSubviews = YES;
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

contentView.userIntractionEnabled = YES;
[self.view addSubView:contentView];

答案 2 :(得分:2)

您可以在添加imageView后在UIButton中添加buttonWithType:UIButtonTypeCustom(类型为UIView)。

  UIImageView *imgview=[[UIImageView alloc]init];
  imgview.backgroundColor=[UIColor blackColor];
  imgview.contentMode=UIViewContentModeScaleToFill;
  UIButton *theButton=[UIButton buttonWithType:UIButtonTypeCustom];
  theButton.tag=i;
  [theButton addTarget:self action:@selector(buttonClicked:) 
  forControlEvents:UIControlEventTouchDown];
  imgview.frame = CGRectMake(x, y, 95, 115);
  [self.view addSubview:imgview];
  theButton.frame = CGRectMake(x, y, 95, 115);
  [imgview addSubview:theButton];

答案 3 :(得分:1)

添加imageView后,只需在View中添加UIButton(类型为RoundedRectType)。

答案 4 :(得分:0)

您可以将其添加为

UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame=CGRectMake(....); // specify the frame of the button as per your requirement
[imageView addSubview:myButton];
imageView.userInteractionEnabled=YES;`

您需要将图片视图的用户互动设置为TRUE,因为默认情况下,互动为FALSE