I have issue where my app has a screen that scans a UPC code and shows the product detail screen if a match found, however the screen shows up as a modal and there is no back button, it looks like this and if I make it full screen you can’t leave this screen.
Here is it looks when accessing via the search screen
Here is how it should look with back button
Here is the code I’m using the present the product detail screen:
func launchApp(decodedURL: String) {
if presentedViewController != nil {
return
}
let decodedUPC = Int(decodedURL)
guard let goodProduct = ProductsProvider().queryUPC(upcNumber: decodedUPC!) else {
let alertController = UIAlertController(title: "UPC Code not found (String(describing: decodedUPC))", message: "We could not find this item, please use our search", preferredStyle: .alert)
let confirmAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: { (action) -> Void in
// Code here
self.dismiss(animated: true, completion: nil)
})
alertController.addAction(confirmAction)
present(alertController, animated: true, completion: nil)
return
}
let viewController:
ProductDetailView = UIStoryboard(
name: "Main", bundle: Bundle(for: ProductDetailView.self)
).instantiateViewController(withIdentifier: "productDetailViewController") as! ProductDetailView
viewController.productDetail = goodProduct
viewController.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
viewController.navigationItem.leftItemsSupplementBackButton = true
let navigationController = UINavigationController(rootViewController: viewController)
// navigationController.modalPresentationStyle = .fullScreen
self.present(navigationController, animated: false, completion: nil)
}
Any help to get the back button on the screen would be greatly appreciated
2
Answers
You can use pushViewController approach instead of present:
Hope this helps!
#Method 1
From Apple documentation regarding to pushViewController(_:animated:) Link:
To update your navigation bar and tool bar in order to show the back button you should push the viewController from your existing navigation controller:
instead of:
#Method 2
To dismiss a full screen presented ViewController simply add a UIBarButtonItem to the navigationItem: