简体   繁体   English

iOS 9 Presentation Popover全屏显示

[英]iOS 9 Presenting Popover takes full screen

I am trying to present a PopOver view controller, specifically to show a small filters screen next to a TextField. 我试图呈现一个PopOver视图控制器,专门用于在TextField旁边显示一个小的筛选器屏幕。 However it is showing as a full-screen view controller. 但是,它显示为全屏视图控制器。 filters_button is the one that should trigger the pop-over. filter_button是应该触发弹出窗口的按钮。 Any ideas why this is showing full screen as if it were a normal ViewController? 有什么想法为什么会像普通的ViewController一样全屏显示?

func showFilters(){
    let tableViewController = UITableViewController()
    tableViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
    tableViewController.preferredContentSize = CGSizeMake(20, 20)

    presentViewController(tableViewController, animated: true, completion: nil)

    let popoverPresentationController = tableViewController.popoverPresentationController
    popoverPresentationController?.sourceView = filters_button
    popoverPresentationController?.sourceRect = CGRectMake(0, 0, filters_button.frame.size.width, filters_button.frame.size.height)
}

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    return .None
}

Note: At the top of my class I declare that it conforms the "UIPopoverPresentationControllerDelegate" protocol 注意:在类的顶部,我声明它符合“ UIPopoverPresentationControllerDelegate”协议

Fixed : Given that for the PopOver to work on iPhone devices, you need to set the delegate of popoverPresentationController before the viewController is presented, that way the method below gets called by the delegate. 固定 :鉴于PopOver要在iPhone设备上运行,您需要在显示viewController之前设置popoverPresentationController的委托,这样委托才能调用以下方法。 So add 所以加

    popoverPresentationController?.delegate = self

below 下面

    popoverPresentationController?.sourceRect = filters_button.frame

and move 并移动

    self.presentViewController(filtersVC, animated: true, completion: nil)

to the end of the function. 到功能的末尾。

You should add the following 您应该添加以下内容

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
    return .none
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM