问题
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:
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;
}
来源:https://stackoverflow.com/questions/38865758/uipopover-from-uibutton-for-iphone