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]; 

此外,使用這種方法的時候,我也沒必要爲橫向模式的任何特殊處理。