gpt4 book ai didi

ios - popoverPresentationController 从 iPhone 上的 UITableViewController 模态显示

转载 作者:可可西里 更新时间:2023-11-01 04:50:28 27 4
gpt4 key购买 nike

我在显示 UIViewController 时遇到问题,在 UITableViewController 中显示带有来自 iPhone 的弹出窗口演示文稿。我一直在使用的代码可以显示来自任何其他 UIViewController 的弹出窗口,而不是 iPhone 上的 UITableViewController。它确实在 iPad 上的 UITableViewController 中工作。

当代码被执行时, View 会暂时以模态呈现(如果我没有实现 adaptivePresentationStyleForPresentationController,您可能会想到)然后被黑色覆盖,就好像发生了某种自动布局错误(尽管控制台中没有任何其他指示)。

正如我所提到的,以下内容在 iPhone 上的 UITableViewController 之外工作,并且始终在 iPad 上工作。我确定我遗漏了一些相当简单的东西。有什么想法吗?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
/* Create label */
UILabel *label = [UILabel new];
label.text = @"This should be in a popover.";
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;

/* Add label to new ViewController */
UIViewController *popoverVC = [UIViewController new];
[popoverVC.view addSubview:label];
label.translatesAutoresizingMaskIntoConstraints = NO;
[popoverVC.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]];
[popoverVC.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]];

/* Define ViewController to present as a popover and display */
popoverVC.modalPresentationStyle = UIModalPresentationPopover;
popoverVC.preferredContentSize = CGSizeMake(320, 50);
[self presentViewController:popoverVC animated:YES completion:nil];

/* Grab handle to ViewController's popoverPresentationController after displaying */
UIPopoverPresentationController *popoverController = [popoverVC popoverPresentationController];
if (popoverController == nil)
NSLog(@"popoverController is nil");
else {
popoverController.delegate = self;
popoverController.popoverLayoutMargins = UIEdgeInsetsMake(15, 15, 15, 15);
popoverController.sourceView = tableView;
popoverController.sourceRect = [tableView rectForRowAtIndexPath:indexPath];

self.definesPresentationContext = YES;
}
}

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection
{
NSLog(@"%s", __PRETTY_FUNCTION__);
return (UIModalPresentationNone);
}

FWIW:快速制作此代码原型(prototype)的一种方法是将以上内容从 Xcode 中新的“Master-Detail Application”模板添加到 MasterViewController.m,并定义此消息以避免推送 DetailViewController:

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
return (NO);
}

您还需要设置 MasterViewController.h 以采用 UIPopoverPresentationControllerDelegate 协议(protocol)。

最佳答案

黑色的原因很简单。

您的 Controller 是透明的(您没有为其 View 设置任何背景色)。演示动画使其出现在 TableView 上方,但一旦动画结束,下面的 View 将从层次结构中删除,最终得到窗口背景颜色(默认为黑色)和带有黑色标签的透明 Controller 。

设置

popoverVC.view.backgroundColor = [UIColor yellowColor];

您将看到 Controller 正常运行。

我不确定为什么您的 Controller 没有显示为弹出窗口,因为您根据文档正确地执行了所有操作。然而,移动

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

作为方法的最后一行解决了这个问题。注意:根据文档,这甚至不起作用。

关于ios - popoverPresentationController 从 iPhone 上的 UITableViewController 模态显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35815409/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com