-4

如何在 imageView 上放置一个圆形矩形按钮,

我的图像视图:self.view addSubview:myImage

更新

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

4

5 回答 5

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]; 
于 2012-11-02T08:26:40.107 回答
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];
于 2012-11-02T08:32:47.583 回答
2

您可以在添加 imageView 后添加UIButton(使用 type 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];
于 2012-11-02T08:45:36.407 回答
1

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

于 2012-11-02T08:26:58.980 回答
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.

于 2012-11-02T09:30:13.673 回答