2016-11-25 147 views
1

這是我的PopOver代碼。我有兩個視圖控制器。我在消息傳遞視圖控制器中呈現PopOver,並且需要管道的視圖控制器是PreferencesView控制器。故事板ID也是相同的首選項視圖控制器。 popOver是成功的,但總是覆蓋整個屏幕。即使UIModalPresentationStyle.None。我在這裏做錯了什麼?PopOver視圖控制器覆蓋全屏

func optionClicked(sender:UIBarButtonItem) 
{ 
    print(")show set preference and set reminder option") 


    let preferenceAction: UIAlertAction = UIAlertAction(title: "Set preferences", style: .Default) { action -> Void in 
     self.optionChoosed(true) 
     } 
    let reminderAction: UIAlertAction = UIAlertAction(title: "Set reminder", style: .Default) { action -> Void in 
     self.optionChoosed(false) 
    } 
    let actionSheetController: UIAlertController = UIAlertController(title: kAlertTitle, message: "What you want to do?", preferredStyle: .ActionSheet) 

    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in 
    } 
    actionSheetController.addAction(preferenceAction) 
    actionSheetController.addAction(reminderAction) 
    actionSheetController.addAction(cancelAction) 
    self.presentViewController(actionSheetController, animated: true, completion: nil) 
} 

func optionChoosed(isSetPreference:Bool) 
{ 


    if(isSetPreference) 
    { 
    print("Set preference") 

     let storyboard : UIStoryboard = UIStoryboard(name: "Messaging", bundle: nil) 
     let vc = storyboard.instantiateViewControllerWithIdentifier("PreferencesViewController") as! PreferencesViewController 
     vc.modalPresentationStyle = UIModalPresentationStyle.Popover 
     let popover: UIPopoverPresentationController = vc.popoverPresentationController! 
     popover.barButtonItem?.action = "isSetPreference" 
     popover.delegate = self 
     presentViewController(vc, animated: true, completion:nil) 


    } 

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

    } 
    func dismiss() { 
     self.dismissViewControllerAnimated(true, completion: nil) 
    } 
+0

其中從optionChoosed調用實現這個功能呢?當你點擊欄按鈕時會彈出PopOver? – Sahil

+0

是的,我有兩個動作在酒吧按鈕 – Niroj

+0

你可以顯示完整的代碼? – Sahil

回答

3

我猜你是從情節串連圖板進行SEGUE。如果你這樣做,從storyboard中移除segue,併爲bar按鈕項目創建一個動作,並將此代碼放在那裏。

let storyboard : UIStoryboard = UIStoryboard(name: "Messaging", bundle: nil) 
    let vc = storyboard.instantiateViewControllerWithIdentifier("PreferencesViewController") as! PreferencesViewController 
    vc.modalPresentationStyle = UIModalPresentationStyle.Popover 
    let popover: UIPopoverPresentationController = vc.popoverPresentationController! 
    vc.preferredContentSize = CGSize(width: 200, height: 200) 
    popover.barButtonItem = sender as? UIBarButtonItem 
    popover.delegate = self 
    presentViewController(vc, animated: true, completion:nil) 
+0

這裏發件人是身份不明 – Niroj

+0

你必須通過它的功能。 self.optionChoosed(true,sender)和函數簽名func optionChoosed(isSetPreference:Bool,sender:UIBarButtonItem) – Sahil

+0

yes!我檢查了 – Sahil

0

嘗試添加

vc.preferredContentSize = CGSize(width: 200, height: 200) 

我希望這將有助於

+0

我試過這不起作用 – Niroj

+0

你還可以添加popover.sourceView = base和popover.sourceRect = base.bounds – aahmetbas

+0

它說未解決的標識符基地 – Niroj

2

,你叫酥料餅

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