Popover image gallery not working in iPad iOS Swift - ios

Recently my app has rejected from App Review because I wasn't using Popover.
then I changed my coding into following. but still I'm not getting popup window in simulator.
Always getting normal iPhone photo choosing method and it makes app crash.
Also its not even printing "working".
#IBAction func chooseGallery(sender: UIBarButtonItem) {
imagePicker.sourceType = .PhotoLibrary
//imagePicker.modalPresentationStyle = .Popover
//presentViewController(imagePicker, animated: true, completion: nil)//4
//imagePicker.popoverPresentationController?.barButtonItem = sender
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
self.presentViewController(imagePicker, animated: true, completion: nil)
}
else {
println("Working")// to test this part
imagePicker.modalPresentationStyle = .Popover
presentViewController(imagePicker, animated: true, completion: nil)//4
imagePicker.popoverPresentationController?.barButtonItem = (sender)
imagePicker.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up
imagePicker.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0)
}
}

Your code works fine for me with some modifications, In the following example I going to present a Popover only for iPad just like you.
#IBAction func showNextViewController(sender: UIBarButtonItem) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("NextViewController") as! NexViewController
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
self.presentViewController(nextViewController, animated: true, completion: nil)
}
else {
nextViewController.modalPresentationStyle = .Popover
presentViewController(nextViewController, animated: true, completion: nil)
if let popover = nextViewController.popoverPresentationController {
popover.barButtonItem = sender
popover.permittedArrowDirections = UIPopoverArrowDirection.Up
// to set it size
nextViewController.preferredContentSize = CGSizeMake(200,500)
}
}
}
Because I don't know the class of UIViewController you're trying to present I just made one myself without nothing inside, and I instantiate it in the #IBAction to avoid keep references to it (is just for test purposes).
Just some observations:
When you're going to present popovers you need to set only one of the two follwoing:
barButtonItem
sourceView, sourceRect
In the above example just like you set the barButtonItem you don't need anything more.
I hope this help you.

try this,
picker.modalPresentationStyle = .CurrentContext

Related

Show BlurEffect on manual segue

I have a question regarding blureffects on a view controller. I program all of my segues manually for several reasons. But when I go to a viewcontroller that has a blureffect background, I only see a dark gray background. How can I solve this that the view controller blur effect lays on top of another viewcontroller where the content of the previous viewcontroller can be seen through?
The blureffects are now applied by storyboard with transparant background views.
My code for seque is:
Action:
#IBAction func ToUserSections(_ sender: Any) {
let controller =
Navigation.getDestinationViewControllerWith("User",
controllerName: "UserSectionsVC")
present(controller, animated: false, completion: nil)
}
Class:
class Navigation{
static func getDestinationViewControllerWith(_
storyboardName:String, controllerName:String)-> UIViewController{
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let controller =
storyboard.instantiateViewController(withIdentifier:
controllerName) as UIViewController
return controller
}
}
Hope you can help me with this because it will maximize aesthetics for the app :D
try this.
let viewController : CustomViewControlller = UIStoryboard.getMainStoryboard().instantiateViewController(withIdentifier: "Identifier") as! CustomViewControlller
viewController.modalPresentationStyle = .overCurrentContext
viewController.modalTransitionStyle = .crossDissolve
self.present(viewController, animated: true, completion: { _ in })
Alright for others to know, what worked for me was the following:
At IBAction: adding the following line before presenting
controller.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
Like this:
#IBAction func ToUserSections(_ sender: Any) {
let controller = Navigation.getDestinationViewControllerWith("User", controllerName: "UserSectionsVC")
controller.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
present(controller, animated: false, completion: nil)
}

How do I programmatically present a popover in iOS using Swift?

My goal is to present a UIViewController as a popover programmatically.
As you can see , the transition style is set to Cross Dissolve and the presentation is set to Over current context. So technically if I click the button, the transition will work, my goal is to do it programmatically.
func clickButton() {
//What should I do here?
}
How do I present the popover programmatically on clickButton?
If you are presenting view controller modally. You can do like this.
func clickButton() {
if let vc = self.storyboard?.instantiateViewController(withIdentifier:"YOUR_VIEW_CONTROLLER_ID") {
vc.modalTransitionStyle = .crossDissolve;
vc.modalPresentationStyle = .overCurrentContext
self.present(vc, animated: true, completion: nil)
}
}
You can try this:
let rateViewController = RatePopupVC()
rateViewController.modalTransitionStyle = .crossDissolve
rateViewController.modalPresentationStyle = .overCurrentContext
self.present(rateViewController, animated: true, completion: nil)
If you want to close it programmatically
Inside PopupVC
self.dismiss(animated: true, completion: nil)
Try this :
func clickButton() {
let vc : PopupVC = storyboard!.instantiateViewControllerWithIdentifier("PopupVCID") as! PopupVC
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
self.presentViewController(vc, animated: true, completion: nil)
}
Try following code.
let popupview = storyboard?.instantiateViewController(withIdentifier: "YourViewController")
popupview.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
self.addChildViewController(popupview)
self.view.addSubview(popupview.view)
popupview.didMove(toParentViewController: popupview)

Drop down list ios swift

I want to have a small UItableView that popup when clicked and shows some numbers in the list.
I tried to use popoverPresentationController but it appears full screen for iOS(iPhone) devices.
below is the code for same -
let filterVC = TableViewController(nibName: "TableViewController", bundle: nil)
filterVC.preferredContentSize = CGSize(width: 300, height: 200)
filterVC.modalPresentationStyle = UIModalPresentationStyle.popover
present(filterVC, animated: true, completion: nil)
let popoverPresentationController = filterVC.popoverPresentationController
if let pop = filterVC.popoverPresentationController {
pop.delegate = self
}
popoverPresentationController?.sourceView = sender as? UIView
popoverPresentationController?.sourceRect = sender.frame
//-------
with below method also
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
// Return no adaptive presentation style, use default presentation behaviour
return .none
}
//-----
Any hint in right direction would be highly appreciated.
working sample would be greatly helpful
What I am trying to achieve as below
UPDATE
There is a useful library you may want to give a try.
It's because your pop.delegate was assigned after you present the filterVC.
Move this
if let pop = filterVC.popoverPresentationController {
pop.delegate = self
pop.sourceView = sender
pop.sourceRect = sender.bounds
}
present(filterVC, animated: true, completion: nil)
to the init of your filterVC should do the trick. Btw, I didn't see anywhere you have assigned sourceView and sourceRect for your popoverPresentationController. Moving pop.delegate = self to this part should be appropriate. Something like
init(for sender: UIView)) {
super.init(nibName: nil, bundle: nil)
modalPresentationStyle = .popover
guard let pop = popoverPresentationController else { return }
pop.sourceView = sender
pop.sourceRect = sender.bounds
pop.delegate = self
}

PopoverPresentationController coming as nil

Created a SingleViewApplication in that I have placed a button.
Now clicking on button I need to display a tableView as popover.
The TableViewController is created in xib.
The issue is tableViewController.popoverPresentationController always comes as nil see below code
let filterVC = TableViewController(nibName: "TableViewController", bundle: nil)
var filterDistanceViewController = UINavigationController(rootViewController: filterVC)
filterDistanceViewController.preferredContentSize = CGSize(width: 300, height: 200)
let popoverPresentationViewController = filterDistanceViewController.popoverPresentationController
popoverPresentationViewController?.permittedArrowDirections = .any
if let pop = filterDistanceViewController.popoverPresentationController {
pop.delegate = self
}
in above code
filterDistanceViewController.popoverPresentationController is always coming as nil
Any hint in right direction will be highly appreciated.
Until you've set a modalPresentationStyle on your VC, the popoverPresentationController property will be nil. Ensure that you set the modalPresentationStyle before accessing.
You are not presenting anything, so you need to present the popoverPresentationViewController on the current viewcontroller, for example:
#IBAction func importantButtonPressed(_ sender: UIButton) {
let tableViewController = UITableViewController()
tableViewController.modalPresentationStyle = .popover
present(tableViewController, animated: true, completion: nil)
if let pop = tableViewController.popoverPresentationController {
pop.delegate = self
}
}
You may do like below.
#IBAction func popoverBtnPressed(_ sender: Any) {
let vc2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController2")
vc2.modalPresentationStyle = .popover
vc2.popoverPresentationController?.delegate = self
vc2.popoverPresentationController?.barButtonItem = popoverBtn
vc2.popoverPresentationController?.sourceRect = .zero
present(vc2, animated: true, completion: nil)
}

How to animate a popover on iPhone, iOS 8

I've got a popover showing up on button click, but it does not animate. I want it to start out at a zero size and grow to be the correct size similar to facebooks carrot action on a post. Any help is very appreciated. Code below:
#IBAction func button(sender: AnyObject) {
let popover = PopoverController()
popover.modalPresentationStyle = .Popover
popover.preferredContentSize = CGSizeMake(UIScreen.mainScreen().bounds.width, 100)
let popoverPresentationController = popover.popoverPresentationController
popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up
popoverPresentationController?.delegate = self
popoverPresentationController?.sourceView = sender as UIView
popoverPresentationController?.sourceRect = sender.bounds
self.presentViewController(popover, animated: true, completion: nil)
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
You can try
popoverPresentationController?.modalTransitionStyle = .crossDissolve
just before presenting.

Resources