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)
Related
How to present UIViewController() inside of UINavigationController ? My controller presenting not fullscreen. I want my app to look like this enter image description here, but it is end up like this enter image description here
I making app without Storyboard(Programmatically)
let customVC = UIViewController()
customVC.view.backgroundColor = .green
customVC.modalPresentationStyle = .fullScreen
let navVC = UINavigationController(rootViewController: customVC)
self.present(navVC, animated: true, completion: nil)
let customVC = DestinationController()
let navVC = UINavigationController(rootViewController: customVC)
// this line overFullScreen
navVC.modalPresentationStyle = .overFullScreen
// for more style
navVC.modalTransitionStyle = .crossDissolve
self.present(navVC, animated: true, completion: nil)
your class where you want to go or present
class DestinationController:UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
}
}
From my onboarding viewcontroller i need to transition to my tabBarController which is also a navigationController and i want to make it as a root viewcontroller afterwards.
#objc func willGoToMain(sender: UIButton!) {
let tabBarController = TabBarController()
let navigationController = UINavigationController(rootViewController: tabBarController)
navigationController.isNavigationBarHidden = true
self.present(tabBarController, animated: true, completion: nil)
}
Thread 1: "Application tried to present modally a view controller <MyStarterProject.TabBarController: 0x7f9bd8011400> that has a parent view controller <UINavigationController: 0x7f9bd7022800>
I like to transition it like a modal presentation or cross dissolve. not just to appeared as a rootviewcontroller all of a sudden.
Here's what i'm trying to do and this solves my problem.
#objc func willGoToMain(sender: UIButton!) {
guard let window = UIApplication.shared.keyWindow else {
return
}
let tabbarController = TabBarController()
let navigationController = UINavigationController(rootViewController: tabbarController)
navigationController.isNavigationBarHidden = true
window.rootViewController = navigationController
window.makeKeyAndVisible()
let options: UIView.AnimationOptions = .transitionCrossDissolve
let duration: TimeInterval = 0.3
UIView.transition(with: window, duration: duration, options: options, animations: {}, completion:
{ completed in
// maybe do something on completion here
})
}
Change this:
self.present(tabBarController, animated: true, completion: nil)
with this:
self.present(navigationController, animated: true, completion: nil)
Please try this one.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainTabBarController = storyboard.instantiateViewController(identifier: "MainTabBarController")
mainTabBarController.modalPresentationStyle = .fullScreen
self.present(mainTabBarController, animated: true, completion: nil)
If you need more details. please visit this blog
https://fluffy.es/how-to-transition-from-login-screen-to-tab-bar-controller/
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'm trying to present a view controller and dismiss my current modal view. I saw the answers on StackOverflow, but I don't understand it.
self.dismissViewControllerAnimated(true, completion: {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("myViewController")
self.presentViewController(vc!, animated: true, completion: nil)
})
Could anyone please tell me, how to do it simply? Thanks
Check your main controller in stack and pop all other view controllers and then present new View Controller.
if let controllers = self.navigationController?.viewControllers {
for vc in controllers {
// Check if the view controller is of MainViewController type
if let myVC = vc as? MainViewController {
self.navigationController?.popToViewController(myVC, animated: false)
}
}
}
Push view controller :
let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "secondVC") as! SecondViewController
self.navigationController?.pushViewController(secondVC, animated: true)
or Present View Controller :
let next = self.storyboard?.instantiateViewController(withIdentifier: "secondVC") as! SecondViewController
self.present(next as UIViewController, animated: true, completion: nil)
You can use this code to Present viewcontroller.
let yourVC = UIStoryboard(name: storyBord, bundle: nil).instantiateViewController(withIdentifier:ID) as! yourViewController
let navController = UINavigationController(rootViewController: yourVC)
self.present(navController, animated: true, completion: {
})
And for dissmissing use this in your presented viewController
self.dismiss(animated: true, completion: {
})
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