UIPopoverController for iphone in ios8 shows white

2019-04-28 23:25发布

问题:

Using UIPopovercontroller below ios8.0 in iphone working fine with this code. But in ios8 it display white screen.

Code :

pickerController = [[UIViewController alloc] init];
UIView *viewV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 160)];
[viewV setBackgroundColor:[UIColor clearColor]];
popOverController = [[UIPopoverController alloc] initWithContentViewController:pickerController];
popOverController.popoverContentSize = CGSizeMake(150, 160);
[popOverController setDelegate:self];

CGRect ImageBtnFrame = [self.view convertRect:sender.frame fromView:self.view];       
[popOverController presentPopoverFromRect:ImageBtnFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

Any alternative for ios8, need suggestion.

回答1:

Try to use the new iOS 8 API for popovers.

pickerController = [[UIViewController alloc] init];
UIView *viewV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 160)];
[viewV setBackgroundColor:[UIColor clearColor]];

UIPopoverPresentationController *popOverController = pickerController .popoverPresentationController;
popOverController.popoverContentSize = CGSizeMake(150, 160);
[popOverController setDelegate:self];

popOverController.sourceView = self.view;
popOverController.sourceRect = sender.frame;
popOverController.permittedArrowDirections = UIPopoverArrowDirectionUp;

[self presentViewController:popOverController
                     animated:YES
                   completion:nil];


回答2:

According to 2014 WWDC, in the 30 minute mark, the right answer is:

- (void) tapButton:(id) sender
{
    MyViewControllerClass * vc = [[MyViewControllerClass alloc] init];
    vc.modalPresentationStyle = UIModalPresentationPopover;

    UIPopoverPresentationController * popOverController = vc.popoverPresentationController;
    [popOverController setDelegate:self];
    popOverController.sourceView = sender;
    popOverController.sourceRect = ((UIButton*)sender).frame;
    popOverController.permittedArrowDirections = UIPopoverArrowDirectionUp;

    [self presentViewController:vc
                       animated:YES
                     completion:nil];
}

Notice that the accepted answer has some problems like:

  • Not setting the modalPresentationStyle
  • Presenting the UIPopoverPresentationController * instead of the UIViewController


回答3:

That will cause on iOS 8. So, I recommended to use following Github library. https://github.com/skywinder/ActionSheetPicker-3.0 or you can write code base on iOS version.

    if (OLDER_THAN_IOS_8) {
       // Your regular code
       pickerController = [[UIViewController alloc] init];
       UIView *viewV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 160)];
       [viewV setBackgroundColor:[UIColor clearColor]];
       popOverController = [[UIPopoverController alloc] initWithContentViewController:pickerController];
       popOverController.popoverContentSize = CGSizeMake(150, 160);
       [popOverController setDelegate:self];

      CGRect ImageBtnFrame = [self.view convertRect:sender.frame fromView:self.view];       
      [popOverController presentPopoverFromRect:ImageBtnFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

    } else {

     // New Code that support iOS -8 using UIPopoverPresentationController
     // Checkout @AndreasZ answer for the same
    }