UIPopOver From UIButton for iPhone

2019-09-03 08:50发布

Can we create iPad like UIPopOver in iPhone devices using Swift where source is UIButton. I created UIPopOver from UIBarButtonItem and its working fine but It's not working in case of UIButton. Can anyone suggest a better solution asap. I need to work it in iPhone device.

1条回答
我命由我不由天
2楼-- · 2019-09-03 09:16

Yes you can, but you need to implement delegate of popoverPresentationController

let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("AnnotationInfoViewController") as! PopUpViewController

//If you want to show view from XIB file you can create viewController instance like this
//let viewController = UIViewController()
//let myView = NSBundle.mainBundle().loadNibNamed("PopUpView", owner: self, options: nil)[0] as! PopUpView
//viewController.view = myView

//Pass the information that you want to show
viewController.data = passdata

//Set the viewController for popoverPresentationController 
viewController.modalPresentationStyle = .Popover
viewController.preferredContentSize = CGSize(width: viewController.view.frame.size.width, height: 80)
viewController.popoverPresentationController!.delegate = self;
viewController.popoverPresentationController!.permittedArrowDirections = [.Up , .Down]
viewController.popoverPresentationController!.sourceView = yourBtn!
viewController.popoverPresentationController!.sourceRect = yourBtn.frame
self.presentViewController(viewController, animated: true, completion: nil)

Now implement this method of popoverPresentationController inside your ViewController

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    return .None;
}
查看更多
登录 后发表回答