UIPopoverPresentationController is showing full sc

2019-04-13 10:11发布

问题:

On iPad UIPopoverPresentationController working fine but on iPhone it is always showing full window modal popup. i am using following code:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MySecondViewController *contentVC = [storyboard instantiateViewControllerWithIdentifier:@"Pop"];
contentVC.modalPresentationStyle = UINavigationControllerOperationPop; // 13
UIPopoverPresentationController *popPC = contentVC.popoverPresentationController; // 14
contentVC.popoverPresentationController.sourceRect =CGRectMake(100, 130, 280, 230);
self.navigationController.preferredContentSize = CGSizeMake(200, self.parentViewController.childViewControllers.lastObject.preferredContentSize.height-100);
//self.showPop.frame; // 15
contentVC.popoverPresentationController.sourceView =
self.showPop; // 16
popPC.permittedArrowDirections = UIPopoverArrowDirectionAny; // 17
popPC.delegate = self; //18
[self presentViewController:contentVC animated:YES completion:nil];

-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}

回答1:

In ViewController.h Firstly make a property of UIPopoverPresenatationController.

 @property(nonatomic,retain)UIPopoverPresentationController *dateTimePopover8;

Then to show PopOverPresentationcontroller

 UINavigationController *destNav = [[UINavigationController alloc] initWithRootViewController:dateVC];/*Here dateVC is controller you want to show in popover*/
            dateVC.preferredContentSize = CGSizeMake(280,200);
            destNav.modalPresentationStyle = UIModalPresentationPopover;
            _dateTimePopover8 = destNav.popoverPresentationController;
            _dateTimePopover8.delegate = self;
            _dateTimePopover8.sourceView = self.view;
            _dateTimePopover8.sourceRect = [sender frame];
            destNav.modalPresentationStyle = UIModalPresentationPopover;
            destNav.navigationBarHidden = YES;
            [self presentViewController:destNav animated:YES completion:nil];

You must have noticed that we are presenting View Controller instead of presenting popOver.So we have to hide this in new way also.It hides automatically when we click on screen.

-(void)hideIOS8PopOver
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

We have to implement the delegate of UIPopoverPresenatationController in implementation file.Write below delegate method in implementation file.

- (UIModalPresentationStyle) adaptivePresentationStyleForPresentationController: (UIPresentationController * ) controller {
    return UIModalPresentationNone;
}


回答2:

Popover controllers are for use exclusively on iPad devices.

Edit: As stated by Soberman, since iOS 8 it is possible to present popovers on iPhone using public APIs, so this answer is probably not relevant anymore.

As stated in Apple's documentation on UIPopoverController:

Popover controllers are for use exclusively on iPad devices. So there is no way to use this class in iPhone application unfortunately. But there are a couple of custom third-party implementations of the functionality provided by UIPopoverController which add iPhone support and more. See https://github.com/50pixels/FPPopover for example.

Edit: There also is another highly customizable popover implementation for both iPhone/iPad worth checking out: https://github.com/nicolaschengdev/WYPopoverController.