ViewController 内容大小不会改变弹出框大小

ViewController content size doesn't change popover size

弹出窗口的首选大小仅在高度大于 320 时有效。

我已经搜索了很多来解决这个问题。但是没有找到任何答案。在 iPad 中是否有弹出窗口应满足的最小尺寸?如果没有,我缺少什么?

代码:

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];

注: 我试过了,adaptivePresentationStyleForPresentationController: & adaptivePresentationStyleForPresentationController: 到 return UIModalPresentationNone.

我尝试了以下方法:

- (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;
}

结果是 (iPad Air 2, iOS 11.2):

这是一个基于您的示例的解决方案,我认为您使用 GCD 显示弹出框可能是导致问题的原因...

    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);

我的弹出窗口有时不可见,因为 sourceRect 不正确。

因为我在导航控制器中嵌入了 ViewController,所以 preferredContentSize 应该设置为导航控制器。

controller.preferredContentSize = CGSizeMake(375, 100);

& 在 ViewController:

self.navigationController.preferredContentSize = contentsize;

并将 sourceRect 更改为:

CGRect rectCustomLoc = [table rectForFooterInSection:section];
rectCustomLoc.size.width = sender.frame.size.width;
popController.sourceRect = rectCustomLoc;