2015-10-21 79 views
0

我想在不同的位置呈現我的popover。默認情況下,它從用戶選擇按鈕的地方開始。我使用的UIViewController而不是UIPopoverController因爲UIPopoverController在iOS9棄用popover改變位置的起點(原點位置)iOS9

class CustomPop: UIViewController, UIPopoverPresentationControllerDelegate { 

init() { 
    super.init(nibName: nil, bundle: nil) 
    self.modalPresentationStyle = UIModalPresentationStyle.Popover; 
    self.popoverPresentationController?.delegate = self; 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

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

//for example something like this but I need it for a popover: 
custom = CustomPop() 
custom.frame.origin.y = self.view.height - 100 
// is there a way to use CGRectMake for the popover to accomplish this? 
+0

你想要的酥料餅是在精確的位置,或者也許你想要的箭頭要放在彈出窗口的左邊大小還是中間或左邊或類似的東西? – Korpel

回答

0

新的解決方案的iOS 8+

// Create Controller 

var popoverContent = CustomPop() 

// Get NavController 
var nav = UINavigationController(rootViewController: popoverContent) 
nav.modalPresentationStyle = UIModalPresentationStyle.Popover 

// Get PopoverPresentationController 
var popover = nav.popoverPresentationController as UIPopoverPresentationController 
popover.sourceView = self.view 
popover.sourceRect = CGRectMake(100,100,0,0) 

// Show Popover 
self.presentViewController(nav, animated: true, completion: nil) 

使用UIPopoverController 棄用

// Create Controller 
UIPopoverController *pc = [[UIPopoverController alloc] initWithContentViewController:vc]; 

// Set Point 
[pc presentPopoverFromRect:CGRectMake(0, 0, 100, 100) 
        inView:self.view 
        permittedArrowDirections:UIPopoverArrowDirectionAny 
        animated:YES]; 
+0

我沒有選擇使用presentpopoverfromrect – SlopTonio

+0

爲什麼不呢?你不能使用UIPopoverController? – SnowMax

+0

我在問題中添加了更多的代碼來展示我如何創建popover – SlopTonio

1
custom.popoverPresentationController?.sourceRect = CGRectMake(100,100,100,100) 

CGRectMake(x.origin,y.origin,寬,高)

+0

您還需要設置popover控制器的'sourceView'。 –

+0

是啊我將sourceView = sender設置爲?UIView – SlopTonio

0

迅速3.1試試這個:

MZFormSheetPresentationController.appearance().movementActionWhenKeyboardAppears = .alwaysAboveKeyboard