This what I use to present popover:
func presentPopoverController(controller: UIViewController, fromView view: UIView) {
controller.modalPresentationStyle = UIModalPresentationStyle.Popover
controller.preferredContentSize = CGSizeMake(400, 400)
let popover = controller.popoverPresentationController
popover?.permittedArrowDirections = .Any
print(view.frame)
popover?.sourceView = view
popover?.sourceRect = view.frame //***
presentViewController(controller, animated: true, completion: nil)
}
This is how it looks on screen:
And this is output on console:
(326.0, 15.0, 63.0, 10.0)
If I remove the line with ***then it looks like following, and is still in wrong place:
Output on console is same as above.
There should be popover?.sourceRect = view.bounds instead of view.frame.
Related
I am trying to create number pad in iPad using popover view controller. Everything is achieved but there is a shadow beneath the pop view. I tried to remove that shadow but nothing worked for me. Here is my code which presents pop over view in my view controller.
let vc = self.storyboard?.instantiateViewController(withIdentifier: "PopOverVC") as? PopOverVC
vc?.modalPresentationStyle = .popover
vc?.preferredContentSize = CGSize(width: 280, height: 425)
vc?.delegate = self
if let popoverPresentationController = vc?.popoverPresentationController {
popoverPresentationController.permittedArrowDirections = [.down, .up, .right, .left]
popoverPresentationController.sourceView = self.view
popoverPresentationController.sourceRect = txtNumber.frame
popoverPresentationController.delegate = self
if let popoverController = vc {
present(popoverController, animated: false, completion: nil)
}
}
Can anybody help me removing the shadow? Thanks in advance!!
I am NOT USING StoryBoard in my project. I have presented one view controller as popOver Style.
let vcOne = ViewControllerOne()
vcOne.showCorrectionFactor = true
vcOne.modalPresentationStyle = .popover
let pop = vcOne.popoverPresentationController
pop?.sourceView = self.view
pop?.sourceRect = sender.frame
vcOne.preferredContentSize = CGSize(width: 400, height: 500)
self.present(vcOne, animated: true, completion: nil)
Now i want to navigate to other view controller from ViewControllerOne() and with in this pop up.
I am doing it like
let vc = PairViewController()
self.navigationController?.pushViewController(vc, animated: true)
But self.navigationController? is getting nil value. How can i achieve this without story board.
You did not embed ViewControllerOne in a UINavigationController, so of course self.navigationController is nil.
Just do this:
// embed it!
let navController = UINavigationController(rootViewController: vcOne)
// get the popover presentation controller from the navigation controller!
let pop = navController.popoverPresentationController
pop?.sourceView = self.view
pop?.sourceRect = sender.frame
vcOne.preferredContentSize = CGSize(width: 400, height: 500)
// present navController rather than vcOne!
self.present(navController, animated: true, completion: nil)
I am trying to use UIPopoverPresentationController to display a popover that doesn't take up the whole screen. already checked this and other tutorials, but don't worked.
Here my code:
#IBAction func temp(_ sender: UIButton) {
let vc = UIStoryboard(name: "StayView", bundle: nil).instantiateViewController(withIdentifier: "StayViewPopOverViewController") as! StayViewPopOverViewController
vc.modalPresentationStyle = .popover
vc.preferredContentSize = CGSize(width: 180, height: 75)
let popover = vc.popoverPresentationController!
popover.sourceView = self.btnTemp
popover.sourceRect = self.btnTemp.bounds
popover.delegate = self
self.present(vc, animated: true, completion: nil)
}
My delegate method:
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle {
return .none
}
but that cover Whole screen.
I tried to put breakpoint on delegate method but interpreter didn't stop on that.
can anyBody have any solution or any suggetions?
Upated: I want to achieve like this:
Finally I Got the answer.
Just need to update my delegate method like this:
func adaptivePresentationStyle(
for controller: UIPresentationController,
traitCollection: UITraitCollection)
-> UIModalPresentationStyle {
return .none
}
Thats It... Works Great!!!
Update following lines
vc.modalPresentationStyle = .popover
vc.preferredContentSize = CGSize(width: 180, height: 75)
with
vc.modalPresentationStyle = .overFullScreen
vc.preferredContentSize = view.frame.size
I'm presenting a popover viewController on top of a UITableView. How can i dismiss this popover when tapped outside of it? I'm trying to call it from the didSelectRow method, but the tap isn't detected. Any suggestion?
Thanks!
this is my code:
let addFriendsPopoverViewController = storyboard?.instantiateViewController(withIdentifier: "HomePopOver") as! HomePopOverViewController
addFriendsPopoverViewController.index = (sender.tag)!
addFriendsPopoverViewController.delegate = self
addFriendsPopoverViewController.isModalInPopover = true
addFriendsPopoverViewController.modalPresentationStyle = UIModalPresentationStyle.popover
addFriendsPopoverViewController.preferredContentSize = CGSize(width: 210, height: 40)
let popoverMenuViewController = addFriendsPopoverViewController.popoverPresentationController
popoverMenuViewController!.permittedArrowDirections = .down
popoverMenuViewController!.delegate = self
popoverMenuViewController!.sourceView = self.view
popoverMenuViewController!.sourceRect = CGRect(
x: UIScreen.main.bounds.width - 105,
y: 50,
width: 1,
height: 1)
present(
addFriendsPopoverViewController,
animated: true,
completion: nil)
A view controller presented in a UIPopoverController, or presented using UIModalPresentationPopover, will automatically be dismissed when the user taps outside the popover, unless you have set isModalInPopover or implemented the delegate method which prevents it.
If you need some code to run when this happens, then you'll need to implement a delegate method too.
Which specific methods depend on whether you're using a UIPopoverController or UIModalPresentationPopover. The tags on your question suggest the former, but that's quite an old-fashioned (and deprecated) way of doing it.
Even though you have found your answer, I wanted to share my solution because I had the same problem and after I spent some time to figure it out, I have found what the problem was. It was not actually because of something missing but because of something is implemented in vain.
Although I don't know why it happens but if following method implemented in your code, that may cause popover not to dismiss by tapping anywhere else than UIBarButtonItem itself.
func prepareForPopoverPresentation(_ popoverPresentationController: UIPopoverPresentationController) {
popoverPresentationController.permittedArrowDirections = .any
popoverPresentationController.barButtonItem = UIBarButtonItem(customView: categoryButton)
}
Here's the rest of implementation I have made for popover:
extension SearchViewController:UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
func presentPopover(sender: UIButton) {
let popoverContentController = PopoverCategoryViewController()
let nav = UINavigationController(rootViewController: popoverContentController)
nav.modalPresentationStyle = UIModalPresentationStyle.popover
nav.isNavigationBarHidden = true
let popover = nav.popoverPresentationController
popover?.backgroundColor = ChaptifyColor.backgroundGray
popover?.delegate = self
popover?.sourceView = sender
popover?.sourceRect = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: sender.bounds.width, height: sender.bounds.height))
popoverContentController.preferredContentSize = CGSize(width: 300, height: 300)
self.present(nav, animated: true, completion: nil)
}
}
I have a view controller that I want to present as a popover. How can I change its size?
let carsViewController = CarsViewController()
carsViewController.modalPresentationStyle = .Popover
if let popoverPresentationController = a carsViewController.popoverPresentationController {
popoverPresentationController.permittedArrowDirections = .Up
popoverPresentationController.sourceView = carsButton
presentViewController(alertsViewController, animated: true, completion: nil)
}
It took me some time but I finally found an answer to this. It is as simple as adding the following line after initialising the view controller.
carsViewController.preferredContentSize = CGSize(width: 220,height:90)