TabbarController not hiding when opening CameraController (BarCodeScanner) - swift

I have a BarCodeScanner-viewController which I call from 3 different views. My app also has a tabbarController. Problem is, the tabbar hides from two of the viewControllers, while the third one always shows the tabbarController, while in cameraMode (barCodeScanner).
I've tried to set the ´self.tabBarController?.tabBar.isHidden = true´ in both viewDidLoad(), viewDidAppear() and viewWillAppear() and changed it to false on viewWillDisappear()
I have also tested to set 'scanner.hidesBottomBarWhenPushed = true' without result.
// working:
setUpBackButton(withTitle: NSLocalizedString("button_cancel", comment: ""))
let scanner = BarCodeScanner()
self.navigationController?.pushViewController(scanner, animated: true)
scanner.callback = { result in
// code with result
}
// working:
setUpBackButton()
let scanner = BarCodeScanner()
scanner.modalPresentationStyle = .overCurrentContext
self.navigationController?.pushViewController(scanner, animated: true)
scanner.callback = { result in
// code with result
}
// NOT WORKING (i.e. not hiding the tabbarController):
let scanner = BarCodeScanner()
setupBackButton()
scanner.modalPresentationStyle = .overCurrentContext
self.navigationController?.pushViewController(scanner, animated: true)
scanner.callback = { result in
// code with result
}
I wan't the tabbar to be hidden in the third example too.

Using Push actually adds a new controller in navigationController thats why your tabbar is not hiding to hide it with new controllers overlay you need to change push with present function in Thrid example
Replace
self.navigationController?.pushViewController(scanner, animated: true)
With
self.navigationController?.present(scanner, animated: true, completion: nil)

Related

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 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)
}
}

Tab Bar not hiding using UIViewController

I am adding a UIViewController as PresentViewController using Tab Bar, then the view looks like:
After selecting any option, I need to hide the Tab Bar, but my screen looks like:
Kindly suggest me the solution for it. I am adding a view controller:
self.presentViewController(responseSelector, animated: true,
completion: nil)
I got solution for it.
let responseSelector =
ResponseSelectorViewController(responseId:currentResponse.title,
delegate:self)
responseSelector.modalPresentationStyle = .OverCurrentContext
self.presentViewController(responseSelector, animated: true, completion: nil)
I have change 1 line of code in this.
responseSelector.modalPresentationStyle = .OverFullScreen
I have used the line below in the ViewWillAppear() function:
self.tabBarController!.tabBar.isHidden = true
And it works great.

Attempt to present View Controller which is not in window hierarchy

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..

Howto Size the Modal View programmatically (SWIFT)

I simply want to present a small option dialog over an existing main UIViewController/UIView , so that on an IPad I would see a small Dialog and in the Background I will see the Main View.
I managed to show a UIViewController/UIView in a modal view style as follow:
func showoptions(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("Options") as! UIViewController
controller.modalPresentationStyle = UIModalPresentationStyle.Popover
let popoverPresentationController = controller.popoverPresentationController
// result is an optional (but should not be nil if modalPresentationStyle is popover)
if let _popoverPresentationController = popoverPresentationController {
// set the view from which to pop up
_popoverPresentationController.sourceView = self.view;
//_popoverPresentationController.sourceRect = CGRectMake(60, 100, 500, 500)
//_popoverPresentationController. .setPopoverContentSize(CGSizeMake(550, 600), animated: true)
//_popoverPresentationController.sourceView.sizeToFit();
// present (id iPhone it is a modal automatic full screen)
self.presentViewController(controller, animated: true, completion: nil)
}
}
But I have still some issues:
1. Howto get rid of the arrow shown at the border.
2. Howto size this modal view. It is shown to small and I would like to fit it to the largest controls in the UIControllerView/UIView.
any help ?
Try changing _popoverPresentationController.permittedArrowDirections to empty option set
Change controller.preferredContentSize to match your desired size
I needed something similar and ended up presenting a view controller as a modal with a transparent background over the current context. There I made a smaller opaque UIView with what I wanted.