添加到UIImageView时,UIButton没有响应
我试图添加一个button到一个透明背景的UIImageView – button显示确定,但没有响应。 (takePictureClicked不叫)
我的代码:
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;
通常你创build一个UIView
,你可以添加不同的UI元素,比如UIButton
或者UIImageView
。 一个UIImageView
只是UIImages
而不是添加任何其他的东西。 创build另一个将保存所有元素的UIView
。
就像这篇文章描述的那样 ,甚至Interface Builder
也不允许你添加一个UIButton
到UIImageView
。
您必须能够进行用户交互:
[cameraOverlayView setUserInteractionEnabled:YES];
如果你使用的话,你也有Interface Builder中的checkbox。
这是需要的,因为默认情况下 , UIImageView
disabled
了用户交互。
你有没有尝试过设置userInteractionEnabled
到UIImageView
?
尝试与:
cameraOverlayView.userInteractionEnabled = YES;