2

The popover's preferred size is only works if the height is greater than 320.

I have searched a lot to solve this issue. But didn't find any answer. Is there any minimum size that the popover should satisfy in iPad? If not, what i'm missing?

Code:

UIViewController *viewController = [subStoryboard instantiateViewControllerWithIdentifier:SMFormViewControllerSBID];
controller = [[UINavigationController alloc] initWithRootViewController:viewController];

SMFormViewController *formViewController = (SMFormViewController *)controller.topViewController;
formViewController.modalPresentationStyle = UIModalPresentationPopover;
formViewController.preferredContentSize = CGSizeMake(375, 320);

popController = [controller popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionLeft;
popController.delegate = self;

popController.sourceView = tblDocDetails;
NSInteger section = [tblDocDetails numberOfSections] - 1;
CGRect rectCustomLoc = [tblDocDetails rectForFooterInSection:section];
popController.sourceRect = CGRectMake(rectCustomLoc.origin.x, rectCustomLoc.origin.y, 130, rectCustomLoc.origin.y/2);

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

Note: I tried, adaptivePresentationStyleForPresentationController: & adaptivePresentationStyleForPresentationController: to return UIModalPresentationNone.

Lal Krishna
  • 15,485
  • 6
  • 64
  • 84

2 Answers2

1

I tried the following:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    UIButton *btnOpen = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnOpen setFrame:CGRectMake(100, 100, 100, 44)];
    [btnOpen setTitle:@"POP" forState:UIControlStateNormal];
    [btnOpen setBackgroundColor:[UIColor grayColor]];
    [btnOpen addTarget:self action:@selector(btnOpen:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnOpen];
}

- (void)btnOpen:(id)sender {
    UIView *sourceView = (UIButton *)sender;

    UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    vc.modalPresentationStyle = UIModalPresentationPopover;
    vc.preferredContentSize = CGSizeMake(150, 150);
    [self presentViewController:vc animated:YES completion:nil];

    UIPopoverPresentationController *popVc = vc.popoverPresentationController;
    popVc.permittedArrowDirections = UIPopoverArrowDirectionLeft;
    popVc.sourceView = sourceView;
    popVc.sourceRect = sourceView.bounds;
}

and it resulted in this (iPad Air 2, iOS 11.2):

150x150 popover

Here is a solution based on your example, I think your use of GCD to show the popover might be causing the issue...

    UIViewController *viewController = [subStoryboard instantiateViewControllerWithIdentifier:SMFormViewControllerSBID];
    controller = [[UINavigationController alloc] initWithRootViewController:viewController];
    controller.modalPresentationStyle = UIModalPresentationPopover;
    controller.preferredContentSize = CGSizeMake(375, 320);

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

    popController = [controller popoverPresentationController];
    popController.permittedArrowDirections = UIPopoverArrowDirectionLeft;
    popController.delegate = self;

    popController.sourceView = tblDocDetails;
    NSInteger section = [tblDocDetails numberOfSections] - 1;
    CGRect rectCustomLoc = [tblDocDetails rectForFooterInSection:section];
    popController.sourceRect = CGRectMake(rectCustomLoc.origin.x, rectCustomLoc.origin.y, 130, rectCustomLoc.origin.y/2);
Mani
  • 1,597
  • 15
  • 19
  • @LalKrishna I think I've found your problem. The issue is that you set the "preferredContentSize" and "modalPresentationStyle" on "formViewController" instead of "controller". Since "controller" is the top-most viewcontroller - the size and presentation style should be set on it. – Mani Mar 05 '18 at 13:22
  • i tried setting, `controller.modalPresentationStyle = UIModalPresentationPopover;` too. & it's still Not Working. – Lal Krishna Mar 05 '18 at 13:40
  • @LalKrishna I will post an alternative solution that you can try in a minute. – Mani Mar 05 '18 at 13:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166295/discussion-between-lal-krishna-and-emmanuel-ay). – Lal Krishna Mar 06 '18 at 08:51
  • 1
    My popover sometimes doesn't visible, because of the `sourceRect` was incorrect. So `controller.preferredContentSize = CGSizeMake(375, 100);` doesn't make change to popover size. Anyway I voted up for you found that `preferredContentSize` to the navigation controller. Thanks! – Lal Krishna Mar 14 '18 at 05:17
1

My popover sometimes doesn't visible, because of the sourceRect was incorrect.

Since I embedded ViewController in a Navigation Controller, preferredContentSize should set to Navigation Controller.

controller.preferredContentSize = CGSizeMake(375, 100);

& in ViewController:

self.navigationController.preferredContentSize = contentsize;

And Changed sourceRect to:

CGRect rectCustomLoc = [table rectForFooterInSection:section];
rectCustomLoc.size.width = sender.frame.size.width;
popController.sourceRect = rectCustomLoc;
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84