ios - 将 UISearchBar 结果呈现为 UIPopoverPresentationController iOS 9

标签 ios swift popover ios9

我有一个带有 UISearchBar 的 ParentViewController 和一个带有 tableView 的 ChildTableViewController,其中将显示搜索结果。当我点击搜索栏时,弹出窗口应显示符合搜索栏上写入的过滤器的所有结果。这意味着,一开始,所有结果都应该显示在 popoverController 中。

问题是结果显示占据整个屏幕,而不是显示在弹出窗口中。下面是与应显示弹出框的 ParentViewController 相对应的代码。

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    let childController = ChildTableViewController()
    childController.modalPresentationStyle = .Popover
    childController.preferredContentSize = CGSize(width: 50, height: 100)
    let popover = childController.popoverPresentationController
    popover?.permittedArrowDirections = .Any
    popover?.delegate = self
    popover?.sourceView = self.view
    popover?.sourceRect = CGRect(x: 200, y: 200, width: 1, height: 1)
    presentViewController(childController, animated: true,completion: nil)

}

enter image description here

我使用的是 Xcode 7 和 iOS 9。

最佳答案

您需要做的是实现以下方法,该方法是 UIPopoverPresentationControllerDelegate 协议(protocol)的一部分:

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone; // This will force a popover display
}

关于ios - 将 UISearchBar 结果呈现为 UIPopoverPresentationController iOS 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32719402/

相关文章:

ios - TabBarController - 第一个 View 偏移 20px

ios - Swift 无法解析 JSON : Could not cast value of type '__NSCFDictionary' to 'NSArray' error

css - 更改 Bootstrap 弹出窗口的宽度

ios - 支持的方向与应用程序没有共同的方向,并且 [RCTModalHostViewController shouldAutorotate] 返回 YES

ios - 如何在另一个实例变量上使用实例变量的属性

popover - iOS 8 - UIPopoverPresentationController 移动弹出框

ios - Swift:尝试从数据源结构中检索数据

iphone - iOS 应用程序中的注音符号

ios - 了解 UIViewController 转换的值(value)

swift - 如何将按钮添加到 CollectionView 的末尾(不在最后一个单元格中)?