2015-10-21 50 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