0

I'm trying to add a button to a UIImageView with a transparent background - the button is shown ok but is not responding. (takePictureClicked is not called)

My code:

cameraController = [[UIImagePickerController alloc] init];
cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraController.delegate = self;
cameraController.allowsEditing=YES;
cameraController.showsCameraControls = NO;
cameraController.navigationBarHidden = YES;
cameraController.toolbarHidden = YES;
// overlay on top of camera lens view
UIImageView *cameraOverlayView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"camera_overlay.png"]];
cameraOverlayView.alpha = 0.0f;
//Take picture button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage *btnImage = [UIImage imageNamed:@"714-camera.png"];
[button setImage:btnImage forState:UIControlStateNormal];
[button addTarget:self action:@selector(takePictureClicked) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(cameraOverlayView.center.x-16,cameraOverlayView.frame.size.height,32,32);
[cameraOverlayView addSubview:button];
[cameraOverlayView setUserInteractionEnabled:YES];
[cameraOverlayView bringSubviewToFront:button];
cameraController.cameraOverlayView = cameraOverlayView;
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Doron Goldberg
  • 643
  • 1
  • 9
  • 23

3 Answers3

0

Normally you create a UIView where you can add different ui elements like UIButton or UIImageView. An UIImageView is just for UIImages and not for adding any other stuff on it. Create another UIView which will hold all the elements in it.

Like this post describes, even Interface Builder doesn't allow you to add a UIButton to an UIImageView.

Community
  • 1
  • 1
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
  • How can I do that? which type should I create and put the UIImage and UIButton on it? If I understood correctly I need to put both controls in a parent control which will be added to the UIImagePickerController as overlay? – Doron Goldberg May 13 '14 at 12:41
  • I didn't work with UIImagePickerController but normally you have a `UIView` as parent which includes everything else and the `UIView` itself can be added to your `UIViewController`. – Alex Cio May 13 '14 at 12:45
0

You have to able the user interaction:

[cameraOverlayView setUserInteractionEnabled:YES];

You have also the checkbox in Interface Builder if you are using it.

This is needed because for default, the UIImageView has the user interaction disabled.

Matteo Gobbi
  • 17,697
  • 3
  • 27
  • 41
0

Have you tried setting userInteractionEnabled to the UIImageView?

try with:

cameraOverlayView.userInteractionEnabled = YES;
Alessandro Orrù
  • 3,443
  • 20
  • 24