iPad上的UIImagePicker中的黑条 - 仅在横向右方位

时间:2011-12-07 21:13:04

标签: objective-c xcode ipad uiimagepickercontroller uipopovercontroller

我通过UIPopOver显示UIImagePicker(本身在UIView中)。这很好用,但是当iPad旋转到右侧时,我会在弹出框的左侧看到一个奇怪的黑条,如下图所示。取消按钮也部分偏离屏幕右侧。在任何其他奇怪的方向都不会发生这种情况。

下面还列出了代码。任何人都可以建议我为什么会得到这个黑条?

imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);

UIViewController *containerController = [[UIViewController alloc] init];
containerController.contentSizeForViewInPopover = CGSizeMake(width, height);
[imagePickerController.view setFrame:containerController.view.frame];
[containerController.view addSubview:imagePickerController.view];



if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    Class cls = NSClassFromString(@"UIPopoverController");
    if (cls != nil) {

        popoverController = [[UIPopoverController alloc] initWithContentViewController:containerController];

        [popoverController presentPopoverFromRect:selectedRect inView:self.view permittedArrowDirections:4 animated:YES];

        [containerController release];

    }

Screenshot

3 个答案:

答案 0 :(得分:3)

由于一些奇怪的原因,视图的框架确实在横向上发生了变化。

要完成此操作,请在之后设置框架,以显示弹出窗口(下面的视图代码)。

这应该可以解决问题。

imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);

UIViewController *containerController = [[UIViewController alloc] init];
containerController.contentSizeForViewInPopover = CGSizeMake(width, height);

[containerController.view addSubview:imagePickerController.view];



if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    Class cls = NSClassFromString(@"UIPopoverController");
    if (cls != nil) {

        popoverController = [[UIPopoverController alloc] initWithContentViewController:containerController];

        [popoverController presentPopoverFromRect:selectedRect inView:self.view permittedArrowDirections:4 animated:YES];

        [imagePickerController.view setFrame:containerController.view.frame];

        [containerController release];

    }

此外,在您的控制器中,添加此项以在旋转发生时重置帧:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {

    [imagePickerController.view setFrame:imagePickerController.view.superview.frame];
}

答案 1 :(得分:1)

上述方法适用于iOS 5及更高版本,但在iOS 4和之前,它无法将UIImagePickerController添加为子视图然后显示它。它总是必须呈现为ModalViewController。

答案 2 :(得分:-1)

试试这个。重新发布我使用[currentDevice endGeneratingDeviceOrientationNotifications]找到的一个解决方案

UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease]; 
picker.sourceType = UIImagePickerControllerSourceTypeCamera;  
picker.delegate = self;

// Display the camera.
[self presentModalViewController:picker animated:YES];

// Given by default your orientation is in landscape right already
while ([currentDevice isGeneratingDeviceOrientationNotifications])
   [currentDevice endGeneratingDeviceOrientationNotifications];

来源:Disable rotation in UIImagePicker