34

我想始终ViewController在所有设备和所有方向上的弹出窗口中显示 a 。我试图通过采用UIPopoverPresentationControllerDelegateand 设置sourceViewand来实现这一点sourceRect

这适用于所有设备和方向,除了 iPhone 6 Plus 横向。在这种情况下,视图控制器会在表单中从屏幕底部向上滑动。我怎样才能防止它总是出现在弹出窗口中?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let popoverPresentationController = segue.destinationViewController.popoverPresentationController
popoverPresentationController?.delegate = self
popoverPresentationController?.sourceView = self.titleLabel!.superview
popoverPresentationController?.sourceRect = self.titleLabel!.frame }

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None }

所有设备均在 iOS 8.2 或更高版本下

4

4 回答 4

81

实现新adaptivePresentationStyleForPresentationController:traitCollection:方法UIAdaptivePresentationControllerDelegate

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
    // This method is called in iOS 8.3 or later regardless of trait collection, in which case use the original presentation style (UIModalPresentationNone signals no adaptation)
    return UIModalPresentationNone;
}

UIModalPresentationNone告诉演示控制器使用原始演示样式,在您的情况下将显示一个弹出框。

于 2015-05-27T11:47:44.097 回答
6

在 Swift 3 中,如果您实现了原始adaptivePresentationStyle方法,只需添加以下代码即可:

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
    return adaptivePresentationStyle(for: controller)
}
于 2016-11-24T20:20:50.090 回答
1

Apple 根据其尺寸等级设计了 iPhone 6 Plus 演示文稿以采用这种方式。

为了防止 iPhone 6 Plus 上的模态显示,您必须覆盖特征集合(水平尺寸)。

您应该能够设置overrideTraitCollection演示控制器的属性:

presentedVC.presentationController.overrideTraitCollection = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact];

(对不起Objective C!我还没学过Swift。)

于 2015-05-22T15:57:54.270 回答
1

给对此有问题的人的说明:

这个

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *) controller traitCollection:(UITraitCollection *)traitCollection {
    return UIModalPresentationNone;
}

和这个不一样

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController: (UIPresentationController * ) controller {
    return UIModalPresentationNone;
}

后者不会被调用/与前者一样工作。

于 2019-11-05T16:23:35.753 回答