I'm trying to add a UIPopoverView to my Swift iOS 8 app, but I am unable to access the PopoverContentSize property, as the popover does not show in the correct shape. my code:
var popover: UIPopoverController? = nil
func addCategory() {
var newCategory = storyboard.instantiateViewControllerWithIdentifier("NewCategory") as UIViewController
var nav = UINavigationController(rootViewController: newCategory)
popover = UIPopoverController(contentViewController: nav)
popover!.setPopoverContentSize(CGSizeMake(550, 600), animated: true)
popover!.delegate = self
popover!.presentPopoverFromBarButtonItem(self.navigationItem.rightBarButtonItem, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
}
output:
When I am doing the same thing through UIPopoverPresentationController, I still don't get it done. this is my code:
func addCategory() {
var popoverContent = self.storyboard.instantiateViewControllerWithIdentifier("NewCategory") as UIViewController
var nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
var popover = nav.popoverPresentationController as UIPopoverPresentationController
popover.delegate = self
popover.popoverContentSize = CGSizeMake(1000, 300)
popover.sourceView = self.view
popover.sourceRect = CGRectMake(100,100,0,0)
self.presentViewController(nav, animated: true, completion: nil)
}
I get the exact same output.
How do I customize the size of my popover? Any help would be highly appreciated!
Okay, A housemate took a look at it and figured it out:
func addCategory() {
var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("NewCategory") as UIViewController
var nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
var popover = nav.popoverPresentationController
popoverContent.preferredContentSize = CGSizeMake(500,600)
popover.delegate = self
popover.sourceView = self.view
popover.sourceRect = CGRectMake(100,100,0,0)
self.presentViewController(nav, animated: true, completion: nil)
}
That's the way.
You don't talk to the popover itself anymore, you talk to the view controller inside of it to set the content size, by calling the property preferredContentSize
Actually it is much simpler than that. In the storyboard you should make the viewcontroller you want to use as popover and make a viewcontroller class for it as usual. Make a segue as shown below from the object you want to open the popover, in this case the UIBarButton named "Config".
In the "mother viewcontroller" implement the UIPopoverPresentationControllerDelegate and the delegate method:
func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {
//do som stuff from the popover
}
Override the prepareForSeque method like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//segue for the popover configuration window
if segue.identifier == "yourSegueIdentifierForPopOver" {
if let controller = segue.destinationViewController as? UIViewController {
controller.popoverPresentationController!.delegate = self
controller.preferredContentSize = CGSize(width: 320, height: 186)
}
}
}
And you're done. And you can now treat the popover view as any other view, ie. add fields and what not! And you get hold of the the content controller by using the popoverPresentationController.presentedViewController method in the UIPopoverPresentationController.
Also on an iPhone you would have to overwrite
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.none
}
I found a complete example of how to get this all to work so that you can always display a popover no matter the device/orientation https://github.com/frogcjn/AdaptivePopover_iOS8_Swift.
The key is to implement UIAdaptivePresentationControllerDelegate
func adaptivePresentationStyleForPresentationController(PC: UIPresentationController!) -> UIModalPresentationStyle {
// This *forces* a popover to be displayed on the iPhone
return .None
}
Then extend the example above (from Imagine Digital):
nav.popoverPresentationController!.delegate = implOfUIAPCDelegate
Swift 2.0
Well I worked out. Have a look. Made a ViewController in StoryBoard. Associated with PopOverViewController class.
import UIKit
class PopOverViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.preferredContentSize = CGSizeMake(200, 200)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "dismiss:")
}
func dismiss(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
See ViewController:
// ViewController.swift
import UIKit
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate
{
func showPopover(base: UIView)
{
if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("popover") as? PopOverViewController {
let navController = UINavigationController(rootViewController: viewController)
navController.modalPresentationStyle = .Popover
if let pctrl = navController.popoverPresentationController {
pctrl.delegate = self
pctrl.sourceView = base
pctrl.sourceRect = base.bounds
self.presentViewController(navController, animated: true, completion: nil)
}
}
}
override func viewDidLoad(){
super.viewDidLoad()
}
#IBAction func onShow(sender: UIButton)
{
self.showPopover(sender)
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .None
}
}
Note: The func showPopover(base: UIView) method should be placed before ViewDidLoad. Hope it helps !
In iOS9 UIPopoverController is depreciated. So can use the below code for Objective-C version above iOS9.x,
- (IBAction)onclickPopover:(id)sender {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
UIViewController *viewController = [sb instantiateViewControllerWithIdentifier:#"popover"];
viewController.modalPresentationStyle = UIModalPresentationPopover;
viewController.popoverPresentationController.sourceView = self.popOverBtn;
viewController.popoverPresentationController.sourceRect = self.popOverBtn.bounds;
viewController.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:viewController animated:YES completion:nil]; }
Here i Convert "Joris416" Swift Code to Objective-c,
-(void) popoverstart
{
ViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"PopoverView"];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:controller];
nav.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popover = nav.popoverPresentationController;
controller.preferredContentSize = CGSizeMake(300, 200);
popover.delegate = self;
popover.sourceView = self.view;
popover.sourceRect = CGRectMake(100, 100, 0, 0);
popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:nav animated:YES completion:nil];
}
-(UIModalPresentationStyle) adaptivePresentationStyleForPresentationController: (UIPresentationController * ) controller
{
return UIModalPresentationNone;
}
Remember to ADD
UIPopoverPresentationControllerDelegate, UIAdaptivePresentationControllerDelegate
This is best explained on the iOS8 Day-by-Day blog
In short, once you've set your UIViewController's modalPresentationStyle to .Popover, you can get hold of a UIPopoverPresentationClass (a new iOS8 class) via the controller's popoverPresentationController property.
I made an Objective-C version of Imagine Digitals swift answer above. I don't think I missed anything as it seems to work under preliminary testing, if you spot something let me know, and I'll update it
-(void) presentPopover
{
YourViewController* popoverContent = [[YourViewController alloc] init]; //this will be a subclass of UIViewController
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:popoverContent];
nav.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController* popover = nav.popoverPresentationController;
popoverContent.preferredContentSize = CGSizeMake(500,600);
popover.delegate = self;
popover.sourceRect = CGRectMake(100,100,0,0); //I actually used popover.barButtonItem = self.myBarButton;
[self presentViewController:nav animated:YES completion:nil];
}
my two cents for xcode 9.1 / swift 4.
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
override func viewDidLoad(){
super.viewDidLoad()
let when = DispatchTime.now() + 0.5
DispatchQueue.main.asyncAfter(deadline: when, execute: { () -> Void in
// to test after 05.secs... :)
self.showPopover(base: self.view)
})
}
func showPopover(base: UIView) {
if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "popover") as? PopOverViewController {
let navController = UINavigationController(rootViewController: viewController)
navController.modalPresentationStyle = .popover
if let pctrl = navController.popoverPresentationController {
pctrl.delegate = self
pctrl.sourceView = base
pctrl.sourceRect = base.bounds
self.present(navController, animated: true, completion: nil)
}
}
}
#IBAction func onShow(sender: UIButton){
self.showPopover(base: sender)
}
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle{
return .none
}
and experiment in:
func adaptivePresentationStyle...
return .popover
or:
return .pageSheet....
and so on..
Implement UIAdaptivePresentationControllerDelegate in your Viewcontroller.
Then add :
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle{
return .none
}
The following has a pretty comprehensive guide on how to configure and present popovers. https://www.appcoda.com/presentation-controllers-tutorial/
In summary, a viable implementation (with some updates from the original article syntax for Swift 4.2), to then be called from elsewhere, would be something like the following:
func showPopover(ofViewController popoverViewController: UIViewController, originView: UIView) {
popoverViewController.modalPresentationStyle = UIModalPresentationStyle.popover
if let popoverController = popoverViewController.popoverPresentationController {
popoverController.delegate = self
popoverController.sourceView = originView
popoverController.sourceRect = originView.bounds
popoverController.permittedArrowDirections = UIPopoverArrowDirection.any
}
self.present(popoverViewController, animated: true)
}
A lot of this was already covered in the answer from #mmc, but the article helps to explain some of those code elements used, and also show how it could be expanded.
It also provides a lot of additional detail about using delegation to handle the presentation style for iPhone vs. iPad, and to allow dismissal of the popover if it's ever shown full-screen. Again, updated for Swift 4.2:
func adaptivePresentationStyle(for: UIPresentationController) -> UIModalPresentationStyle {
//return UIModalPresentationStyle.fullScreen
return UIModalPresentationStyle.none
}
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
if traitCollection.horizontalSizeClass == .compact {
return UIModalPresentationStyle.none
//return UIModalPresentationStyle.fullScreen
}
//return UIModalPresentationStyle.fullScreen
return UIModalPresentationStyle.none
}
func presentationController(_ controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
switch style {
case .fullScreen:
let navigationController = UINavigationController(rootViewController: controller.presentedViewController)
let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: #selector(doneWithPopover))
navigationController.topViewController?.navigationItem.rightBarButtonItem = doneButton
return navigationController
default:
return controller.presentedViewController
}
}
// As of Swift 4, functions used in selectors must be declared as #objc
#objc private func doneWithPopover() {
self.dismiss(animated: true, completion: nil)
}
Hope this helps.
var popoverContent = (self.storyboard?.instantiateViewController(withIdentifier: "WhatsNewViewController"))! as UIViewController
var nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.popover
var popover = nav.popoverPresentationController
popoverContent.preferredContentSize = CGSize(width: 500, height: 100)
popover?.delegate = self
popover?.sourceView = self.view
popover?.sourceRect = CGRectMake(100,100,0,0)
self.present(nav, animated: true, completion: nil)
For those who wants to study!
I created an Open Source project for those who want to study and use Popover view for anypurpose. You can find the project here. https://github.com/tryWabbit/KTListPopup
Related
I'm trying to present UINavigationController as a small window using the following way. It works fine on iPads but it still displays fullScreen on iPhones. Any tips on what I am doing wrong will be greatly appreciated!
class LoginNewNavigationController: UINavigationController{
private var windowSize: CGSize!
init() {
let rootVc = LoginNewIPadViewController.init()
super.init(rootViewController: rootVc)
self.configureSizes()
self.preferredContentSize = self.windowSize
self.modalPresentationStyle = .formSheet
self.showNavigationBar = false
self.view.backgroundColor = .clear
self.view.layer.cornerRadius = 25
}
}
A "smaller window" presentation is not built in on iPhone. You need a custom presentation controller that dictates a smaller size and placement of the presented view controller.
It's the LoginnewIPadViewController that needs to have it's modal presentation style set not the navigation controller. Also there is no need to reference self the scope is clear for the compiler.
class LoginNewNavigationController: UINavigationController{
private var windowSize: CGSize!
init() {
let rootVc = LoginNewIPadViewController.init()
rootVc.modalPresentationStyle = .formSheet
super.init(rootViewController: rootVc)
configureSizes()
preferredContentSize = windowSize
showNavigationBar = false
view.backgroundColor = .clear
view.layer.cornerRadius = 25
}
}
Not possible with a formSheet on iPhone, it can be achieved using .popover style. See following example -
class LoginNewIPadViewController: UIViewController {}
class LoginNewNavigationController: UINavigationController {
private var windowSize: CGSize!
init() {
let rootVC = LoginNewIPadViewController.init()
super.init(rootViewController: rootVC)
self.preferredContentSize = self.windowSize
self.modalPresentationStyle = .popover
if let presentationController = self.popoverPresentationController {
/// Configure the way you want this to be shown
// presentationController.sourceRect = ??
// presentationController.sourceView = ??
// presentationController.barButtonItem = ??
presentationController.permittedArrowDirections = .up
presentationController.delegate = self
}
}
}
extension LoginNewNavigationController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}
I don't ask for a lot out of life, but I'd give a lot to know how to get a navigation bar on this popover:
Here's my storyboard:
And here's my code presenting the popover:
#IBAction func doButton(_ sender: Any) {
let vc = PopViewController()
vc.preferredContentSize = CGSize(width: 260,height: 300)
vc.modalPresentationStyle = .popover
vc.view.backgroundColor = UIColor.green
if let pres = vc.presentationController {
pres.delegate = (self as UIAdaptivePresentationControllerDelegate)
}
self.present(vc, animated: true)
if let pop = vc.popoverPresentationController {
pop.sourceView = (sender as! UIView)
pop.sourceRect = (sender as! UIView).bounds
pop.backgroundColor = vc.view.backgroundColor
}
}
}
extension ViewController : UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .none
}
}
(Thanks to #matt, BTW, for the above code!)
I've searched high and low on Google yet haven't found an answer that I could understand. I tried to add the nav bar in Storyboard, but no dice--the only element that would accept it was the prototype cell in the table view.
Please don't redirect to something written 7 years ago--I've already read most of them, and I'm using Swift now. Certainly, I may have overlooked a clear answer, and I'll be humble and contrite if that's the case. But meantime, I'd sure appreciate some help if ya got it!
Thanks!
Change your right tableview controller to a navigation controller with a tableViewController root. The navigation Controller gets an identifier : "navigation".
Now you can change the first code of doButton function, and keep the rest.
#IBAction func doButton(_ sender: Any) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "navigation") as! UINavigationController
vc.preferredContentSize = CGSize(width: 260,height: 300)
vc.modalPresentationStyle = .popover
vc.view.backgroundColor = UIColor.green
if let pres = vc.presentationController {
pres.delegate = (self as UIAdaptivePresentationControllerDelegate)
}
self.present(vc, animated: true)
if let pop = vc.popoverPresentationController {
pop.sourceView = (sender as! UIView)
pop.sourceRect = (sender as! UIView).bounds
pop.backgroundColor = vc.view.backgroundColor
}
}
finally , you will get the nav bar in your popover.
I'm trying to display a ViewController as a popover on an iPhone. I have already been through several answers on SO and the rest of the web but none have worked so far. I wrote a simple app to test this.
ViewController.swift:
import UIKit
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(clicked(_:)))
}
func clicked(_ sender: Any) {
let vc = UIViewController()
vc.view.backgroundColor = UIColor.blue
vc.preferredContentSize = CGSize(width: 200, height: 200)
vc.modalPresentationStyle = .popover
present(vc, animated: true, completion: nil)
let ppc = vc.popoverPresentationController
ppc?.permittedArrowDirections = .any
ppc?.delegate = self
ppc?.barButtonItem = navigationItem.rightBarButtonItem
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .none
}
}
The storyboard has an empty ViewController embedded in a NavigationController.
Running this, I expected a popover view controller to show under the "done" button. Instead, the blue view controller is presented full screen.
Is there a way to change this behaviour?
You are connecting delegate after presenting view. How it will return .none from delegate and show as popover. Use this :-
func clicked(_ sender: Any) {
let vc = UIViewController()
vc.view.backgroundColor = UIColor.blue
vc.modalPresentationStyle = .popover
vc.preferredContentSize = CGSize(width: 200, height: 200)
let ppc = vc.popoverPresentationController
ppc?.permittedArrowDirections = .any
ppc?.delegate = self
ppc?.barButtonItem = navigationItem.rightBarButtonItem
ppc?.sourceView = sender
present(vc, animated: true, completion: nil)
}
import UIKit
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(clicked(_:)))
}
func clicked(_ sender: Any) {
let vc = UIViewController()
vc.view.backgroundColor = UIColor.blue
vc.preferredContentSize = CGSize(width: 200, height: 200)
vc.modalPresentationStyle = .popover
let ppc = vc.popoverPresentationController
ppc?.permittedArrowDirections = .any
ppc?.delegate = self
ppc!.sourceView = sender as? UIView
ppc?.barButtonItem = navigationItem.rightBarButtonItem
present(vc, animated: true, completion: nil)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}
The solutions above no longer work in recent iOS versions 12 and above. To get it working again, override -modalPresentationStyle within the viewController to be presented as a popover and return UIModalPresentationPopover.
Additionally provide a popoverPresentationController.delegate, implement adaptivePresentationStyleForPresentationController:traitCollection: and return UIModalPresentationNone.
#interface PopoverViewController : UIViewController
#end
#implementation PopoverViewController
- (UIModalPresentationStyle)modalPresentationStyle
{
return UIModalPresentationPopover;
}
#end
#interface ViewController ()<UIPopoverPresentationControllerDelegate>
#end
#implementation ViewController
- (IBAction)openPopover:(id)sender
{
UIViewController* testVC = [[PopoverViewController alloc] init];
testVC.view.backgroundColor = UIColor.yellowColor;
UIPopoverPresentationController* popPresenter = [testVC popoverPresentationController];
popPresenter.permittedArrowDirections = UIPopoverArrowDirectionUp;
popPresenter.delegate = self;
popPresenter.sourceView = sender;
popPresenter.sourceRect = [sender bounds];
[self presentViewController:testVC animated:YES completion:^{}];
}
#pragma mark protocol (UIPopoverPresentationControllerDelegate)
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection
{
return UIModalPresentationNone;
}
#end
Add:
vc.popoverPresentationController?.delegate = self
just before the line:
present(vc, animated: true, completion: nil)
I am tired of trying to PopOver the view controller and searched every where, tried myself also.This issue has again arrived and i am confused what to do next
func showPopover(base: UIView)
{
let storyboard : UIStoryboard = UIStoryboard(name: "Messaging", bundle: nil)
if let viewController = storyboard.instantiateViewControllerWithIdentifier("PreferencesViewController") as?PreferencesViewController
{
let navController = UINavigationController(rootViewController: viewController)
navController.modalPresentationStyle = .Popover
if let pctrl = navController.popoverPresentationController
{
pctrl.delegate = self
pctrl.sourceView = base
pctrl.sourceRect = base.bounds
self.presentViewController(navController, animated: true, completion: nil)
}
}
}
I am calling this method in any one of the actions clicked from UIBarButtons
func optionChoosed(hello:Bool)
{
if (hello)
{
self.showPopover(hello)
}
}
it says Cannot convert the value of type BOOL to expected argument UIiew.. can we fix this or am i going wrong direction.
class SHNewStylesViewController: UIViewController, UIPopoverPresentationControllerDelegate {
var popover: UIPopoverPresentationController? = nil
//MARK: - View life cycle
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//MARK: - IBActions
#IBAction func showPopover(sender: AnyObject) {
let genderViewController = storyboard!.instantiateViewControllerWithIdentifier("ViewControllerTwoIdentifier") as! ViewControllerTwo
genderViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
genderViewController.preferredContentSize = CGSize(width: 200.0, height: 400.0) // To change the popover size
popover = genderViewController.popoverPresentationController!
popover!.barButtonItem = sender as? UIBarButtonItem
popover!.delegate = self
presentViewController(genderViewController, animated: true, completion:nil)
}
//MARK: - Popover Presentation Controller Delegate methods
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
}
In my case showPopover is a IBAction of my bar button item. You can you that code inside the showPopover method wherever you want.
Thanks:)
When I try to display a popover view controller programmatically it won't work and I don't know why. I've copied from multiple sources on the web and nothing seems to work, I get the same error in the console every time showing Warning: Attempt to present <AddFriendsPopoverViewController> on <MainPageViewController> whose view is not in the window hierarchy! I am lost and can't seem to figure out what the problem is, thanks in advance!
Here is my swift code in my viewDidLoad() function:
let addFriendsPopoverViewController = AddFriendsPopoverViewController()
override func viewDidLoad() {
super.viewDidLoad()
if (PFUser.currentUser()?["numberOfFriends"])! as! NSObject == 0 {
print(PFUser.currentUser()?["numberOfFriends"])
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("AddFriendsPopoverViewController") as! UIViewController
vc.modalPresentationStyle = UIModalPresentationStyle.Popover
vc.preferredContentSize = CGSizeMake(50, 50)
let popoverMenuViewController = vc.popoverPresentationController
popoverMenuViewController!.permittedArrowDirections = .Any
popoverMenuViewController!.delegate = self
popoverMenuViewController!.sourceView = self.view
popoverMenuViewController!.sourceRect = CGRectMake(
100,
100,
0,
0)
self.presentViewController(vc, animated: true, completion: nil)
}
}
EDIT
I figured out that for a popover to work with iPhone the following code is required.
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle {
// Return no adaptive presentation style, use default presentation behaviour
return .None
}
Your view is not in the view hierarchy until it has been presented and not during viewDidLoad:
Move your code to viewDidAppear:
if (PFUser.currentUser()?["numberOfFriends"])! as! NSObject == 0 {
addFriendsPopoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
addFriendsPopoverViewController.preferredContentSize = CGSizeMake(200, 200)
let popoverMenuViewController = addFriendsPopoverViewController.popoverPresentationController
popoverMenuViewController!.permittedArrowDirections = .Any
popoverMenuViewController!.delegate = self
popoverMenuViewController!.sourceView = self.view
popoverMenuViewController!.sourceRect = CGRect(
x: 50,
y: 50,
width: 1,
height: 1)
presentViewController(
addFriendsPopoverViewController,
animated: true,
completion: nil)
}
Your code is working right but u can not write that presentViewController code in ViewDidLoad method because viewdidLoad call till that time controller itself it not presented thats why it's not allow to presentViewController .
Write that same code in..
override func viewDidAppear(animated: Bool)
{
var controller = UIViewController()
controller.view.backgroundColor = UIColor .greenColor()
presentViewController(controller, animated: true, completion: nil)
}
I have made it simple for multiple use and its ready to use and go. just copy and paste this extension.
extension UIViewController: UIPopoverPresentationControllerDelegate{
#discardableResult func presentPopOver(_ vcIdentifier: String, _ isAnimate: Bool = true,sender:UIView,contentSize:CGSize = .init(width: 100, height: 100)) -> (UIViewController){
let popoverContentController = storyboard?.instantiateViewController(withIdentifier: vcIdentifier)
popoverContentController?.modalPresentationStyle = .popover
if let popoverPresentationController = popoverContentController?.popoverPresentationController {
popoverPresentationController.permittedArrowDirections = .up
popoverPresentationController.sourceView = sender
popoverPresentationController.sourceRect = sender.bounds
popoverContentController?.preferredContentSize = contentSize
popoverPresentationController.delegate = self
if let popoverController = popoverContentController {
present(popoverController, animated: isAnimate, completion: nil)
}
}
return popoverContentController ?? UIViewController()
}
public func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
public func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {
}
public func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool{
return true
}}
**how to use for presenting pop over on button click:-**
#IBAction func sortClicked(_ sender: UIButton) {
presentPopOver("PopOverVC", sender: sender)
}
***presenting pop over and to get and pass data:-***
#IBAction func sortClicked(_ sender: UIButton) {
let vc = presentPopOver("PopOverVC", sender: sender) as? PopOverVC
vc?.arrayNames = ["name1","name2"]
vc?.callBack = {name in
print(name)
vc?.dismiss(animated: true)
}
}