我希望始终在所有设备和所有方向的弹出窗口中显示ViewController
。我尝试通过UIPopoverPresentationControllerDelegate
并设置sourceView
和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或更高版本
答案 0 :(得分:69)
实施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
告诉演示控制器使用原始演示文稿样式,在您的情况下将显示弹出窗口。
答案 1 :(得分:4)
在Swift 3中,如果您实现了原始adaptivePresentationStyle
方法,只需添加此代码即可:
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return adaptivePresentationStyle(for: controller)
}
答案 2 :(得分:1)
Apple根据其尺寸等级设计了iPhone 6 Plus演示文稿。
要防止iPhone 6 Plus上的模式显示,您必须覆盖特征集合(水平尺寸)。
您应该能够为演示控制器设置overrideTraitCollection
属性:
presentedVC.presentationController.overrideTraitCollection = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact];
(抱歉,目标C!我还没有学过Swift。)
答案 3 :(得分:0)
对此有疑问的人的说明:
此
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *) controller traitCollection:(UITraitCollection *)traitCollection {
return UIModalPresentationNone;
}
与此不同
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController: (UIPresentationController * ) controller {
return UIModalPresentationNone;
}
后面的将不会被称为/与前者相同。