Home > database >  Not able to add back button to swift navigation controller
Not able to add back button to swift navigation controller

Time:09-30

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.

Screen without back button

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

CodePudding user response:

You can use pushViewController approach instead of present:

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "identifier_of_target_vc") as! ClassNameofTargetVC 
self.navigationController?.pushViewController(nextViewController, animated:true)

Hope this helps!

CodePudding user response:

#Method 1

From Apple documentation regarding to pushViewController(_:animated:) Link:

In addition to displaying the view associated with the new view controller at the top of the stack, this method also updates the navigation bar and tool bar accordingly.

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:

self.navigationController?.pushViewController(viewController, animated: false)

instead of:

let navigationController = UINavigationController(rootViewController: viewController)
self.present(navigationController, animated: false, completion: nil)

#Method 2

To dismiss a full screen presented ViewController simply add a UIBarButtonItem to the navigationItem:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Presented View"

        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelAction))
    }

    @objc func cancelAction() {
        dismiss(animated: true)
    }
}
  • Related