I am having an issue with using the .overCurrentContext modalPresentationStyle on a tvOS view controller:
let vc = UIStoryboard(name: "", bundle: Bundle.main).instantiateInitialViewController() //representative of actually presented VC
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: nil)
On the presented view controller, pressing the menu button ceases to return to the presenting view controller. This also occurs when setting it to .overFullScreen and .blurOverFullScreen. However, I am having no such problem when setting it to .currentContext or .fullScreen. Is there anything particular that needs to be used when using certain UIModalPresentationStyle's?
let vc = UIStoryboard(name: "", bundle: Bundle.main).instantiateInitialViewController() //representative of actually presented VC
vc.modalPresentationStyle = .overCurrentContext
self.definesPresentationContext = true //*** adding this line should solve your issue ***
self.present(vc, animated: true, completion: nil)
So what's going on here? The definesPresentationContext property was added in iOS 8, and the documentation states the following:
When a view controller is presented, iOS starts with the presenting view controller and asks it if it wants to provide the presentation context. If the presenting view controller does not provide a context, then iOS asks the presenting view controller's parent view controller. iOS searches up through the view controller hierarchy until a view controller provides a presentation context. If no view controller offers to provide a context, the window's root view controller provides the presentation context.
If a view controller returns YES, then it provides a presentation context. The portion of the window covered by the view controller's view determines the size of the presented view controller's view. The default value for this property is NO.
By setting definesPresentationContext to YES you ensure that the controller to be presented is presented within the bounds of the original view controller.
Related
I am trying to change the transition animation in navigation controller to load a new UIView controller from bottom to top, using Segue. I believe it will not be too hard to implement but may be I am not able to understand it.
My first attempt was to use Hero library but it does not work for some reason.
The answers found so far were only in Objective-C
You can try this solution to present your new controller modally.
let storyboard = UIStoryboard(name: "Storyboard Name", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "Controller Name
")
self.present(controller, animated: true, completion: nil)
I have the next function
func switchRootViewController(rootViewController: UIViewController) {
let window = UIApplication.shared.windows.first!
window.rootViewController = rootViewController
window.makeKeyAndVisible()
}
And I use it in a presented view controller, ex in PresentedViewController.
let navigationViewController = UINavigationController(rootViewController: PresentedViewController())
present(navigationViewController, animated: true, completion: nil)
But when I switch to the needed view controller, my presented view controller doesn't deinitialize. I have to use this way, first dismiss:
self.dismiss(animated: true, completion: {
switchRootViewController(rootViewController: HomeViewController.instantiate())
})
instead of simple
switchRootViewController(rootViewController: HomeViewController.instantiate())
UIViewController objects are retained when presented by another UIViewController. To release your view controllers, they need to be removed from the view hierarchy, which you can accomplish by dismissing them. As far as where to dismiss your view controller, I'm not sure because you haven't given enough information. But you need to dismiss any view controller that you want released from memory.
After presenting a Tab bar controller, I can't dismiss tab bar controller.
I also can't even tap my button after I reinstall without delete the app. Need to uninstall and reinstall the app then I am able to tap the button
I already tried some other way of dismiss the tab bar controller but still unable to dismiss the controller.
This is my current way to present controller after login
let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
let loginVC = storyboard.instantiateViewController(withIdentifier: "Home")
self.present(loginVC, animated: true, completion: nil)
This is my current way to dismiss controller
#IBAction func btnLogout_TouchUpInside(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
This is my root view
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
if defaults.bool(forKey: "isLoggedIn") {
// Show Home screen
window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "Home")
} else {
// Show Login screen
window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "Login")
}
window?.makeKeyAndVisible()
try to dismiss all presented controller it may work for you
DispatchQueue.main.async {
self.view.window!.rootViewController?.dismiss(animated: true, completion: {
print("All controller dismissed successfully..")
})
}
Try This
self.presentingViewController?.dismiss(animated: true, completion: nil)
Hmm. Your update helps although I'm still not sure what's happening. My guess is that you are setting login as root, then presenting home. But I'm not sure where or what you are trying to dismiss. If it's on the login then there is nothing to dismiss as it's the root view controller.
My suggestion would be to reconfigure the storyboard. Because you are manually presenting the view controllers I'm going to assume that the storyboard doesn't contain any segues between the controllers. I'd suggest adding the segues and using them.
I'd set the home view controller as the initial view controller and set the Main storyboard as the storyboard to load in the apps settings. Then all the code to load the storyboard and set the root view controller can be removed.
Next I'd make a manual modal segue from the home view controller to the login view controller. Then in the viewDidAppear of the home view controller I'd add the code to decide if a login was needed and to them perform the login segue.
Then in the login view controller you can do a dismiss and it will remove the model login view.
This is but one of many ways to do this, but it's pretty much the simplest to get you going. You don't need any code to load or set root view controllers or anything else. It just lets the storyboard do the work for you.
My custom AVPlayerViewController won't autorotate if the previous view controller has been instantiated. If I present the AVPlayerViewController, it rotates, and if I present the previous view controller, it rotates. Here's my code for instantiating a view controller :
let vc = self.storyboard!.instantiateViewController(withIdentifier: "MovieVC") as! MovieVC
vc.movie = self.movie
self.navigationController?.pushViewController(vc, animated: true)
In this situation I am creating a copy of the current view controller with different information. If there is no way to fix the autorotating error, is there a different way to present the same view controller?
The problem is that you are not presenting the view controller. You are pushing it. That means that the navigation controller itself is in charge of rotation, so you don't get any special autorotation for this view controller.
The only way to give this view controller its own power over autorotation is to present it:
self.present(vc, animated: true)
I have VC1 which is a basic view controller, I then want to present VC2 which is inside a navigation controller. Whenever I present it, it doesn't display the navigation controller. I want this all done pragmatically. The code I have been using to present VC2 is:
func matchesPressed(sender: UIButton!) {
let matchesTVC: Matches = self.storyboard?.instantiateViewControllerWithIdentifier("Matches") as! Matches
self.presentViewController(matchesTVC, animated: true, completion: nil)
}
How do I present the navigation controller it is inside as well?
Is the navigation controller in the storyboard? If so, give it a storyboard ID and replace your matchesTVC stuff with the navigation controller.
If matches is a standalone view controller in the storyboard, you can do it in code like this:
let matchesTVC: Matches = self.storyboard?.instantiateViewControllerWithIdentifier("Matches") as! Matches
let navContr = UINavigationController(rootViewController:matchesTVC)
self.presentViewController(navContr, animated: true, completion: nil)
Instantiate the navigation controller that contains your view controller and present the navigation controller.