ios - ViewController 内容大小不会改变弹出窗口大小

标签 ios objective-c ipad uipopoverpresentationcontroller

弹出框的首选尺寸仅在高度大于 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: 返回 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):

150x150 popover

这是基于您的示例的解决方案,我认为您使用 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);

关于ios - ViewController 内容大小不会改变弹出窗口大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49110674/

相关文章:

objective-c - Eventkit 无法访问日历

iphone - 为什么我的触摸移动委托(delegate)不起作用?

ios - 当横向打开应用程序时,新创建的 UIWindow 是横向的

iphone - 使用单个 NSMutableArray 填充 UITableView 部分表

objective-c - 手动分配 tableView.rowheight 如何影响给定 TableView 的 subview ?

iphone - 尝试构建非临时版本时出现 Entitlements.plist 错误?

ios - 基于另一个 UIPickerView 显示 UIPickerView

ios - 我想在不使用任何第三方框架的情况下将过滤器应用于录制的视频。我曾尝试将 CIfilter 用于 CAlayer 但它不起作用

objective-c - 在另一个 xib 中从 xib 加载自定义 View 时遇到问题

ios - 连接到不在 "valid connection destination"的 IOS IBAction