UIReferenceLibraryViewController не может быть представлен как всплывающее окно (всегда покрывает весь экран)

Следующий код Swift 3 (кредит на Hacking With Swift) создает UIAlertController и успешно представляет его как всплывающее окно из CGRect координировать на iPad, через API UIPopoverPresentationController:

func popUpToSelectTheLanguage(){
    let ac = UIAlertController(title: "Set expected language...", message: nil, preferredStyle: .actionSheet)
    ac.addAction(UIAlertAction(title: "English", style: .default, handler: { (action) in /* Action to take */ }))
    ac.addAction(UIAlertAction(title: "French", style: .default, handler: { (action) in /* Action to take */ }))
    ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))

    let popover = ac.popoverPresentationController
    popover?.sourceView = view
    popover?.sourceRect = CGRect(x: 32, y: 32, width: 64, height: 64)
    present(ac, animated: true)
}

Тем не менее, при попытке представить другой UIViewController как всплывающее окно UIReferenceLibraryViewController ( одноязычный / двуязычный словарь системы), оно охватывает весь экран, а не отображается как всплывающее окно.

func popUpTheDictionary(){
    let dic = UIReferenceLibraryViewController(term: "hi" as String)

    let popover = dic.popoverPresentationController
    popover?.sourceView = view
    popover?.sourceRect = CGRect(x: 32, y: 32, width: 64, height: 64)
    present(dic, animated: true)
}

В чем здесь проблема? Дальнейшая настройка UIReferenceLibraryViewController требуется, или это просто заблокировано против того, чтобы быть представленным как всплывающее окно?

Моя цель - iOS 10.2, Universal; сборка для iPad Pro (12,9 дюйма).

Если нет способа решить эту проблему, я в равной степени принимаю любой ответ, содержащий код, необходимый для представления UIReferenceLibraryViewController как поповер через другую библиотеку (бонусные баллы, если он работает как поповер на iPhone и iPad).

1 ответ

Решение

Вам нужно установить контроллер вида modalPresentationStyle в .popover,

func popUpTheDictionary(){
    let dic = UIReferenceLibraryViewController(term: "hi" as String)
    dic.modalPresentationStyle = .popover // add this

    let popover = dic.popoverPresentationController
    popover?.sourceView = view
    popover?.sourceRect = CGRect(x: 32, y: 32, width: 64, height: 64)
    present(dic, animated: true)
}

UIAlertController делает это для вас, так как это всегда поповер.

Другие вопросы по тегам