Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPopoverController for iphone in ios8 shows white screen

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.

like image 972
Maniganda saravanan Avatar asked Oct 14 '14 12:10

Maniganda saravanan


3 Answers

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
like image 140
ppaulojr Avatar answered Oct 23 '22 10:10

ppaulojr


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];
like image 41
AndreasZ Avatar answered Oct 23 '22 11:10

AndreasZ


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
    }
like image 2
MilanPanchal Avatar answered Oct 23 '22 10:10

MilanPanchal