I am trying to make a popover menu with the following code:
import UIKit
class BeobachtungViewController: UIViewController, UIPopoverPresentationControllerDelegate {
#IBAction func addClicked(_ sender: AnyObject) {
// get a reference to the view controller for the popover
let popController = UIStoryboard(name: "Personenakte", bundle: nil).instantiateViewController(withIdentifier: "popoverId")
// set the presentation style
popController.modalPresentationStyle = UIModalPresentationStyle.popover
// set up the popover presentation controller
popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
popController.popoverPresentationController?.delegate = self
popController.popoverPresentationController?.sourceView = sender as! UIView // button
popController.popoverPresentationController?.sourceRect = sender.bounds
// present the popover
self.present(popController, animated: true, completion: nil)
}
// UIPopoverPresentationControllerDelegate method
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
// Force popover style
return UIModalPresentationStyle.none
}
}
This is working on iPad, but, on an iPhone, the popup takes the whole iPhone screen. I just want a small window with an arrow. I found several tutorials but none worked for me.
Change your delegate method to:
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
// return UIModalPresentationStyle.FullScreen
return UIModalPresentationStyle.none
}
Related
I am trying to make a popover menu with the following code:
import UIKit
class BeobachtungViewController: UIViewController, UIPopoverPresentationControllerDelegate {
#IBAction func addClicked(_ sender: AnyObject) {
// get a reference to the view controller for the popover
let popController = UIStoryboard(name: "Personenakte", bundle: nil).instantiateViewController(withIdentifier: "popoverId")
// set the presentation style
popController.modalPresentationStyle = UIModalPresentationStyle.popover
// set up the popover presentation controller
popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
popController.popoverPresentationController?.delegate = self
popController.popoverPresentationController?.sourceView = sender as! UIView // button
popController.popoverPresentationController?.sourceRect = sender.bounds
// present the popover
self.present(popController, animated: true, completion: nil)
}
// UIPopoverPresentationControllerDelegate method
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
// Force popover style
return UIModalPresentationStyle.none
}
}
This is working on iPad, but, on an iPhone, the popup takes the whole iPhone screen. I just want a small window with an arrow. I found several tutorials but none worked for me.
Change your delegate method to:
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
// return UIModalPresentationStyle.FullScreen
return UIModalPresentationStyle.none
}
I couldn't show popover controller as popover in iPhone whereas it works very well with iPad.
Any ideas on how to do that in iPhone.
As far as I have searched I couldn't find any.
anyways to make the popover appear in iphone as it is in iPad is appreciated !
Set yourself as the popover view controller's delegate before presenting it, and implement the delegate method adaptivePresentationStyle(for:traitCollection:) to return .none. This will cause the popover to stop adapting on iPhone as a fullscreen presented view controller and turn into an actual popover just like on the iPad.
This is a complete working example that presents the popover in response to a button tap:
class ViewController: UIViewController {
#IBAction func doButton(_ sender: Any) {
let vc = MyPopoverViewController()
vc.preferredContentSize = CGSize(400,500)
vc.modalPresentationStyle = .popover
if let pres = vc.presentationController {
pres.delegate = self
}
self.present(vc, animated: true)
if let pop = vc.popoverPresentationController {
pop.sourceView = (sender as! UIView)
pop.sourceRect = (sender as! UIView).bounds
}
}
}
extension ViewController : UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .none
}
}
I am trying to present a PopOver view controller, specifically to show a small filters screen next to a TextField. However it is showing as a full-screen view controller. filters_button is the one that should trigger the pop-over. Any ideas why this is showing full screen as if it were a normal ViewController?
func showFilters(){
let tableViewController = UITableViewController()
tableViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
tableViewController.preferredContentSize = CGSizeMake(20, 20)
presentViewController(tableViewController, animated: true, completion: nil)
let popoverPresentationController = tableViewController.popoverPresentationController
popoverPresentationController?.sourceView = filters_button
popoverPresentationController?.sourceRect = CGRectMake(0, 0, filters_button.frame.size.width, filters_button.frame.size.height)
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .None
}
Note: At the top of my class I declare that it conforms the "UIPopoverPresentationControllerDelegate" protocol
Fixed:
Given that for the PopOver to work on iPhone devices, you need to set the delegate of popoverPresentationController before the viewController is presented, that way the method below gets called by the delegate. So add
popoverPresentationController?.delegate = self
below
popoverPresentationController?.sourceRect = filters_button.frame
and move
self.presentViewController(filtersVC, animated: true, completion: nil)
to the end of the function.
You should add the following
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .none
}
I know how to present a popover from a bar button item as is described in this answer (for both iPhone and iPad).
I would like to add a popover for an arbitrary anchor point. The other SO answers that I saw were for bar button items or in Objective-C.
I just learned how to do this, so I am adding my own answer below.
Updated for Swift 3
In the storyboard, add a view controller that you would like to be the popover. Set the Storyboard ID to be "popoverId".
Also add a button to your main view controller and hook up the IBAction to the following code.
import UIKit
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
#IBAction func buttonTap(sender: UIButton) {
// get a reference to the view controller for the popover
let popController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popoverId")
// set the presentation style
popController.modalPresentationStyle = UIModalPresentationStyle.popover
// set up the popover presentation controller
popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
popController.popoverPresentationController?.delegate = self
popController.popoverPresentationController?.sourceView = sender // button
popController.popoverPresentationController?.sourceRect = sender.bounds
// present the popover
self.present(popController, animated: true, completion: nil)
}
// UIPopoverPresentationControllerDelegate method
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
// Force popover style
return UIModalPresentationStyle.none
}
}
Setting the sourceView and sourceRect is what allows you to choose an arbitrary point to display the popover.
That's it. Now it should like something like this when the button is tapped.
Thanks to this article for help.
Solution for Swift 3.1 :
Add to your ViewController UIPopoverPresentationControllerDelegate delegate :
class OriginalViewController: UIViewController, UIPopoverPresentationControllerDelegate
Add a button to your ViewController and on tap on your button, call this code :
let controller = MyPopViewController()
controller.modalPresentationStyle = UIModalPresentationStyle.popover
let popController = controller.popoverPresentationController
popController?.permittedArrowDirections = .any
popController?.delegate = self
popController?.sourceRect = (self.myButton?.bounds)!
popController?.sourceView = self.myButton
self.present(controller, animated: true, completion: nil)
Update to func syntax above:
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentatinStyle {
return .none
}
For some reason the old syntax is still allowed but is not active and will not implement popup or anchor correctly.
This question already has an answer here:
iPad detail settings page like popup viewController
(1 answer)
Closed 6 years ago.
I want to present a viewController in UIPopoverPresentationController like iOS Setting App do when we choose language. It presents a viewController at center of device screen!
But, my result is not like that!
This is my code:
func rightButtonPressed(rightBarButton: UIBarButtonItem) {
let detailVC = self.storyboard?.instantiateViewControllerWithIdentifier("ExampleViewController") as! ExampleViewController
detailVC.modalPresentationStyle = .Popover
let popOverVC = detailVC.popoverPresentationController
popOverVC?.permittedArrowDirections = .Any
popOverVC?.barButtonItem = rightBarButton
popOverVC?.sourceView = self.view
popOverVC?.delegate = self
self.presentViewController(detailVC, animated: true, completion: nil)
}
And the delegate:
extension DetailViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
debugPrint(controller.description)
return .Popover
}
func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
let navController = UINavigationController.init(rootViewController: controller.presentedViewController)
return navController
}
}
Try setting the presentation style to sheet:
detailVC.modalPresentationStyle = UIModalPresentation.FormSheet