弹出窗口改变位置起点(原点位置)iOS9



我想在不同的位置显示我的弹出窗口。默认情况下,它从用户选择按钮的位置开始。我使用UIVIewController而不是UIPopoverController因为UIPopoverController在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?
custom.popoverPresentationController?.sourceRect = CGRectMake(100,100,100,100)

CGRectMake (x。原点,原点,宽度,高度)

新解决方案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];

试试这个在迅速3.1 :

MZFormSheetPresentationController.appearance().movementActionWhenKeyboardAppears = .alwaysAboveKeyboard

最新更新