UIModalPresentationPopover для iPhone 6 Plus в ландшафте не отображает popover
Я хочу всегда показывать ViewController
во всплывающем окне на всех устройствах и всех ориентациях. Я попытался выполнить это, приняв UIPopoverPresentationControllerDelegate
и установив sourceView
и sourceRect
.
Это очень хорошо работает для всех устройств и ориентации, за исключением iPhone 6 Plus в ландшафте. В этом случае контроллер просмотра соскальзывает со дна экрана в виде листа. Как я могу предотвратить это, чтобы он всегда появлялся в popover?
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 или выше
Ответы
Ответ 1
Внедрите новый 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
сообщает контроллеру презентации использовать оригинальный стиль презентации, который в вашем случае отобразит popover.
Ответ 2
Apple разработала презентацию iPhone 6 Plus, чтобы вести себя таким образом, основываясь на своем классе размеров.
Чтобы предотвратить модальную презентацию на iPhone 6 Plus, вам придется переопределить коллекцию признаков (размер по горизонтали).
Вы должны установить свойство overrideTraitCollection
для контроллера представления:
presentedVC.presentationController.overrideTraitCollection = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact];
(Извините за Objective C! Я еще не научился Swift.)
Ответ 3
В Swift 3, если вы внедрили оригинальный метод adaptivePresentationStyle
, просто добавьте этот код:
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return adaptivePresentationStyle(for: controller)
}