4

我正在尝试更改UIImagePicker的大小,该UIImagePicker是通过UIPopOver呈现的。代码如下所示。问题在于PopOver尺寸无法正确显示 - 它会以正确的尺寸短暂闪烁,然后缩小为通常的默认尺寸。通过UIPopOver(iPad)提供的UIImagePicker更改大小

有人可以告诉我如何改变popover的大小吗?

- (void)showImagePickerForPhotoLibrary { 
    NSLog(@"Picker"); 
    imagePickerController = [[UIImagePickerController alloc] init]; 
    imagePickerController.delegate = self; 
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    imagePickerController.contentSizeForViewInPopover = CGSizeMake(768, 1000); 

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
     Class cls = NSClassFromString(@"UIPopoverController"); 
     if (cls != nil) { 
      popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePickerController]; 
      //popoverController.popoverContentSize = CGSizeMake(768, 1000); 
      [popoverController presentPopoverFromRect:selectedRect inView:self.view permittedArrowDirections:4 animated:YES]; 
     } 
    } 
    else { 
     [app.mainViewController presentModalViewController:imagePickerController animated:YES]; 
    } 
} 

回答

10

我不知道这是最优雅的解决方案,但它似乎为我工作的罚款:

您可以嵌入的UIImagePickerController的观点变成一个“容器” UIViewController的视图。

- (void)showImagePickerForPhotoLibrary { 
NSLog(@"Picker"); 
    imagePickerController = [[UIImagePickerController alloc] init]; 
    imagePickerController.delegate = self; 
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

    UIViewController *containerController = [[UIViewController alloc] init]; 
    containerController.contentSizeForViewInPopover = CGSizeMake(768, 1000); 

    [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]; 
     } 
    } 
    else { 
     [app.mainViewController presentModalViewController:containerController animated:YES]; 
    } 
} 

希望这有助于

编辑:一些奇怪的原因,该框架确实在景观权当改变。

为了解决这个问题,您需要在显示弹出窗口后设置图像选择器视图的框架。

我编辑了上面的代码来显示这一点。

而且,在你的控制器,补充一点:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 

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

谢谢。无论如何,我可以得到iPad的宽度和高度的行containerController.contentSizeForViewInPopover = CGSizeMake(768,1000); ? – GuybrushThreepwood

+0

iPad的分辨率是768x1024。如果你想让弹出窗口占据整个屏幕,请尝试CGSizeMake(self.view.frame.size.width,self.view.frame.size.height) – Mutix

+0

这类作品,但会导致另一个(奇怪的)错误:http ://stackoverflow.com/questions/8422636/black-bar-in-uiimagepicker-on-ipad-only-in-landscape-right-orientation – GuybrushThreepwood

0

当我试图Mutix的解决方案酥料饼的表现只是一个空白视图导航栏。

我的解决方法是将imagePickerController添加为containerController的子项,而不是仅添加选取器的视图。

主要就意味着替换此行:

[containerController.view addSubview:imagePickerController.view]; 

这些行:

[containerController addChildViewController:imagePickerController]; 
[containerController.view addSubview:imagePickerController.view]; 
[imagePickerController didMoveToParentViewController:containerController]; 

此外,使用这种方法的时候,我也没必要为横向模式的任何特殊处理。