代码之家  ›  专栏  ›  技术社区  ›  Lal Krishna

ViewController内容大小不更改popover大小

  •  2
  • Lal Krishna  · 技术社区  · 7 年前

    popover的首选尺寸仅在高度大于320时有效。

    为了解决这个问题,我已经搜索了很多。但没有找到任何答案。在iPad中,popover有没有满足的最小尺寸?如果没有,我错过了什么?

    代码:

    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: & PresentationController的自适应PresentationStyle: 返回 UIModalPresentationNone

    2 回复  |  直到 7 年前
        1
  •  1
  •   Mani    7 年前

    我尝试了以下方法:

    - (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):

    150x150 popover

    这是一个基于您的示例的解决方案,我认为您使用GCD显示popover可能会导致此问题。。。

        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);
    
        2
  •  1
  •   Lal Krishna    7 年前

    我的popover有时看不见,因为 sourceRect 不正确。

    由于我在导航控制器中嵌入了ViewController, preferredContentSize 应设置为导航控制器。

    controller.preferredContentSize = CGSizeMake(375, 100);
    

    &在里面 视图控制器:

    self.navigationController.preferredContentSize = contentsize;
    

    和已更改 sourceRect 收件人:

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