如何在 iPhone 6 Plus 横屏模式下设置 UIModalPresentationStyle?

3
我希望在所有设备和方向上始终以弹出视图控制器的形式呈现。我尝试通过采用UIPopoverPresentationControllerDelegate并设置sourceView和sourceRect来实现此目的。故事板中的segue被配置为Present As Popover segue。这对于所有设备和方向都非常有效,除了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
}
1个回答

7

实现UIAdaptivePresentationControllerDelegate协议中的新方法(从iOS 8.3开始),即adaptivePresentationStyleForPresentationController:traitCollection::

- (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告诉展示控制器使用原始的展示样式,这种情况下会显示一个弹出窗口。


1
在Xcode 7中,这已不再起作用。文档现在指出,您只能从此方法返回FullScreen和OverFullScreen。 - Jordan H
当然,可以访问以下链接:https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAdaptivePresentationControllerDelegate_protocol/index.html#//apple_ref/occ/intfm/UIAdaptivePresentationControllerDelegate/adaptivePresentationStyleForPresentationController: - Jordan H
实际上,我正在研究与您提到的方法类似的方法。包括 traitCollection 的方法未在文档中列出。但不幸的是,这对我没有用,因为在 iPhone 6 Plus 横向时它没有调用该方法,但在旋转时确实调用了该方法。在纵向时,它在呈现时确实调用了该方法,但返回 .None 会导致呈现方式为 Over Full Screen 而不是弹出窗口。 - Jordan H
3
没关系,这仍然按预期工作。问题是在设置委托之前我就展示了它(文档中指出应该先设置委托)。在显示之前设置 popoverPresentationController 的委托解决了问题。 - Jordan H
@Joey 我在 iPhone 6 Plus 上也遇到了同样的问题,横屏模式,iOS 9(在 iOS 8 上正常工作),我首先设置了委托,然后显示。可能是什么原因呢? - iOS Dev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接