UIButton is not responding when added to a UIImageView

情到浓时终转凉″ 提交于 2019-12-11 18:07:02

问题


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;

回答1:


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.




回答2:


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.




回答3:


Have you tried setting userInteractionEnabled to the UIImageView?

try with:

cameraOverlayView.userInteractionEnabled = YES;


来源:https://stackoverflow.com/questions/23631208/uibutton-is-not-responding-when-added-to-a-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!