通过UIPopOver(iPad)更改UIImagePicker的大小

Guy*_*ood 4 objective-c uiimagepickercontroller ipad uipopovercontroller

我正在尝试通过UIPopOver更改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];
    }   
}
Run Code Online (Sandbox Code Playgroud)

Mut*_*tix 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];
    }   
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助

编辑:由于一些奇怪的原因,框架确实在景观正确时改变.

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

我已经编辑了上面的代码来展示这个.

此外,在您的控制器中,添加以下内容:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {

    [imagePickerController.view setFrame:imagePickerController.view.superview.frame];
}
Run Code Online (Sandbox Code Playgroud)