Attempt to present View Controller which is not in window hierarchy - swift

My initial view controller is created from Storyboard. It has a button which adds a UINavigation :
let navigationController = UINavigationController(rootViewController: ListViewController)
self.presentViewController(navigationController, animated: true, completion:nil)
appDelegate.window?.rootViewController = navigationController
ListViewController has a UICollectionView. Tapping on cell (which I subclassed) calls a delegate method back to ListViewController and presents another View with UICollectionView:
let detailedListViewController = DetailedCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
self.navigationController?.pushViewController(detailedListViewController, animated: true)
Inside this DetailedCollectionView each cell has a button to show a full-screen SFSafariViewController. So as before, using delegate I'm trying to show it like this:
SFSafariVC = SFSafariViewController(URL: urlToLoad)
SFSafariVC.view.frame = self.view.frame
self.presentViewController(SFSafariVC, animated: true, completion: nil)
By doing so, I can see the SFSafariView working correctly, but I get the message:
Warning: Attempt to present < UIAlertController: 0x7fcd96353e20 > on
< UINavigationController: 0x7fcd948a0600 > whose view is not in the
window hierarchy!
I tried changing self.presentViewController to self.navigationController?.presentViewController but still the same error persists..

Related

View on View controllers are all square shaped on Xcode 11, how?

I have tried to change it's size manually (by dragging) and still not working.
You can set its Content size to whatever you want.
On iOS 13 a "Modal" segue will show the presenting controller that way by default.
If you want the view controller to occupy the whole screen, set "Full Screen" on the "presentation" setting of any segue entering that controller.
Segue Settings
If your view presents programmatically (not via storyBoard), have this in mind:
From XCode 11 and up, when you build for iPad you have to add "modalPresentationStyle = .fullScreen" to get full screen views, not square ones. As follows:
let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, completion: nil)
If you want transparency on the new viewController, you can write ".overFullScreen", as follows:
let vc = UIViewController()
vc.modalPresentationStyle = .overFullScreen
self.present(vc, animated: true, completion: nil)

My UIViewcontroller is not filling the entire screen?

I have a very simple app with two UIviewcontrollers. I want to dismiss one and present another one. However, when I do this (code below), the second viewcontroller does not fill the screen, instead it hovers over the top one and can easily get dismissed if you swipe from the top down (you can just about see the first viewcontroller at the top)?
VC-1:
#objc private func picksAction(){
print("picks button pressed")
let layout = UICollectionViewFlowLayout()
let viewController = GridPicksCollectionViewController(collectionViewLayout: layout)
let navController = UINavigationController()
navController.pushViewController(viewController, animated: true)
self.present(navController, animated: true) {}
}
Result:
Set modalPresentationStyle to .fullScreen:
navController.modalPresentationStyle = .fullScreen

Present viewcontroller with tabbar

I created UITabBarController programmatically in AppDelegate with 4 view controllers(using .xib). When user tap some button on ViewController (VC-A) it present another VC (VC-B) and covered tabbar. So I want to VC-B has a tabbar on the button.
I tried to add VC-B as a child of tabbarcontroller. I tried to .present(vc) and .show(vc) on both: VC-A and VC-A.TabBarController
Creating controllers in AppDelegate:
let controllers = [tabViewController1,tabViewController2,tabViewController3,tabViewController4]
tabBarController.viewControllers = controllers
window?.rootViewController = tabBarController
presenting in VC-A
self.tabBarController?.present(controller, animated: false, completion: nil)
right click and drag from tabbar controller in storyboard to VC-B. that should create a tab on the bottom of your VC-A and VC-B to go back and forth without having to implement any backend code unless you want to animate
The solution is to embed every VC in navigationController and then add to TabBarController.
let vc1 = ViewController1()
let navController1 = UINavigationController(rootViewController: vc1)
navController.isNavigationBarHidden = true
let controllers = [navController1, navController2, navController3, navController4]
tabBarController.viewControllers = controllers
window?.rootViewController = tabBarController
Then call
self.navigationController?.pushViewController(controller, animated:
true)
To diplay VC with tabbar
I will press the red login button at the bottom of the picture and try to log in.
login button => "로그인"
After that, log in.
let moreVC = self.storyboard?.instantiateViewController(withIdentifier: "MoreViewController") as! MoreViewController
moreVC.definesPresentationContext = true
moreVC.modalPresentationStyle = .fullScreen
let navController = UINavigationController(rootViewController: moreVC)
self.present(navController, animated: true, completion: nil)
If the login proceeds without error, the above code will be called to display the screen when the login is completed.
If the flow proceeds as the code above, this screen appears.
The screen shown is not fullscreen, and the tabbar at the bottom is gone. The screen I want is the screen below.
How can I present a tab bar when presenting the screen?

Present below current view and not above

I'm trying to present a view controller below another presented view controller (like WhatsApp when you open camera and press gallery).
I tried many things but none worked ..
Use child view controller and set view of that added child view controller at the top of hierarchy. It will be top most element, so actual background of this view will be obscured, but that's the way to go.
//code inside UIViewController class
func addViewControllerAtBottom() {
let newVC = NewVCType() //just instantiate it
addChildViewController(newVC)
view.insertSubview(newVC.view, at: 0) //at 0 means it's first rendered, all others will be on top of it
}
You can reproduce this behavior by doing the following :
First, create a NavigationController with a root ViewController :
let navController = UINavigationController(rootViewController: firstController)
Then, present this navigationController with animated: false and in the completion of the present method, push your second ViewController, still with animated: false (to avoid weird animations) :
present(navController, animated: false) {
navController.pushViewController(secondController, animated: false)
}
Here you go, you got a new navigation with 2 UIViewController, like WhatsApp.
Full code, wrapped into a button's action :
#IBAction func buttonTapped(_ sender: Any) {
let navController = UINavigationController(rootViewController: firstController)
present(navController, animated: false) {
navController.pushViewController(secondController, animated: false)
}
}

modalPresentationStyle .overCurrentContext causing issues with remote button presses on presented view controller

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.