UIPopOver From UIButton for iPhone

独自空忆成欢 提交于 2019-12-12 07:03:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!