1

我通過UIPopOver顯示UIImagePicker(本身在UIView)。這工作正常,但是當iPad旋轉到右側時,我會在彈出窗口的左側獲得一個奇怪的黑色條,如下圖所示。取消按鈕也部分偏離屏幕的右側。這在任何其他奇怪的方向中都不會發生。在iPad的UIImagePicker黑色酒吧 - 只在風景右方

該代碼也在下面列出。任何人都可以提出爲什麼我得到這個黑色的酒吧?

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

一些奇怪的原因,該視圖的框架在做正確的景觀變化。

要過來這個,請在​​之後設置框架您提供了彈出窗口(查看代碼,下面)。

這應該做的伎倆。

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

非常感謝這個答案。 – portoalet

1

上述方法可在iOS 5及以上,但在iOS 4和之前它不可能添加UIImagePickerController作爲子視圖,然後顯示它。它始終必須作爲ModalViewController呈現。

-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