UIImagePickerController覆盖按钮未触发

Chr*_*unn 2 iphone uiimagepickercontroller ios

当用户登陆页面时,我有一个类型为camera的UIImagePickerController。我在UIImagePickerController的顶部放了一个自定义的相机覆盖,但是没有一个按钮事件触发。我知道这些代码大部分来自基本的Apple代码,所以我对正在发生的事情感到困惑。

@interface TakePhotoViewController : UIImagePickerController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
...

- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{
if (self.capturedImages.count > 0)
{
    [self.capturedImages removeAllObjects];
}

self.sourceType = sourceType;
self.delegate = self;

if (sourceType == UIImagePickerControllerSourceTypeCamera)
{
    /*
     The user wants to use the camera interface. Set up our custom overlay view for the camera.
     */
    self.showsCameraControls = NO;

    /*
     Load the overlay view from the OverlayView nib file. Self is the File's Owner for the nib file, so the overlayView outlet is set to the main view in the nib. Pass that view to the image picker controller to use as its overlay view, and set self's reference to the view to nil.
     */
    [[NSBundle mainBundle] loadNibNamed:@"CameraOverlay" owner:self options:nil];
    float scale = [self getScaleForFullScreen];
    self.cameraViewTransform = CGAffineTransformMakeScale(scale, scale);
    self.overlayView.frame = self.cameraOverlayView.frame;
    self.cameraOverlayView = self.overlayView;
    self.overlayView = nil;
}
Run Code Online (Sandbox Code Playgroud)

}

CameraOverlay的所有者是TakePhotoViewController。CameraOverlay内部的按钮之一将“ Touch Up Inside”事件发送到下面列出的插座功能。下面是附属于按钮的TakePhotoViewController中的代码,其中没有触发日志:

- (IBAction)outlet:(id)sender {
NSLog(@"outlet");
}
Run Code Online (Sandbox Code Playgroud)

mat*_*att 5

问题是这一行:

self.overlayView.frame = self.cameraOverlayView.frame;
Run Code Online (Sandbox Code Playgroud)

我们cameraOverlayView为零;它没有框架,因为它甚至还不存在。因此,我们给overlayView零帧。它是无量纲的。该按钮出现,但是它的大小为零,因此无法点击。

现在,您可能会问,为什么呢?这是因为iOS中的触摸屏交付方式。要可触摸,子视图的超级视图也必须是可触摸的,因为该超级视图在子视图之前经过了触摸测试。但是零尺寸的视图是不可触摸的:您的触摸会错过overlayView,因此它也会错过按钮。

解决方案可能像给出overlayView合理的真实框架一样简单。可能有关于时间的问题,当你做到这一点(例如,它可能是你真的不能设置框,直到图像选择器即将出现- viewWillAppear:),但在任何情况下,这是开始的地方。