2015-12-01 58 views
7

我想創建一個彈出式窗口,當我呈現視圖控制器時,背景始終爲純黑色,並且大小爲全屏。如何在iOS9中呈現PopOver

我似乎無法找出什麼是錯的,這裏是代碼,我有

@IBAction func distancePopOver(sender : UIBarButtonItem){ 

    //a UIViewController that I created in the storyboard 
    let controller = storyboard!.instantiateViewControllerWithIdentifier("distancePopOver") 
    controller.modalPresentationStyle= UIModalPresentationSTyle.PopOver 
    controller.preferredContentSize = CGSizeMake(200,30) 
    self.presentViewController(controller, animated: true, completion: nil) 

    //Configure the Popover presentation controller 
    let popController = (controller.popoverPresentationController)! 
    popController.permittedArrowDirections = UIPopoverArrowDirection.Down 
    popController.barButtonItem = sender 
    popController.delegate = self 
    } 

每當我點擊的UIBarButtonItem,它提出了在全屏視圖,但它不應該是我在第5行中指定的尺寸?

回答

1

現在的蛋糕很挑剔。首先,你需要在呈現之前配置popoverPresentationController。

其次,確保您的箭頭方向指向箭頭指向的方向,而不是內容分別對應於UIBarButtonItem的位置。所以,如果它的內部UIToolbar(並且靠近屏幕的底部),您將需要.Down,否則,如果其導航欄(靠近頂部),則需要使用.Up。現在

@IBAction func distancePopOver(sender : UIBarButtonItem){ 

    //Configure the Popover presentation controller 
    let popController = (controller.popoverPresentationController)! 
    popController.permittedArrowDirections = .Down // .Up 
    popController.barButtonItem = sender 
    popController.delegate = self 

    //a UIViewController that I created in the storyboard 
    let controller = storyboard!.instantiateViewControllerWithIdentifier("distancePopOver") 
    controller.modalPresentationStyle = .Popover 
    controller.preferredContentSize = CGSizeMake(200,30) 

    presentViewController(controller, animated: true, completion: nil) 
} 

,如果你有這遠不及它仍然沒有工作,因爲它在一個緊湊的尺寸級別的酥料餅的默認行爲是充滿整個屏幕。由於您已將視圖控制器設置爲彈出窗口的代表,因此您只需實現此代理功能:adaptivePresentationStyleForPresentationController(_:traitCollection:)並返回.None以獲得演示樣式。這將使你甚至可以在iPhone上顯示真正的外觀。查看我的博客文章:iPhone Popover瞭解完整示例。

+1

最後一點與adaptivePresentationStyleForPresentationController(_:traitCollection :)爲我做了。謝謝 –

+0

它在iPad上運行良好,但不在iPhone中運行。 – Satyam