0

I was having this issue and I've tried a lot of solutions that was proposed by some kind people here in the following topic: Swift - How to hide back button in navigation item

I created a ViewController class:

import SwiftUI
import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.setHidesBackButton(true, animated: true)
        self.navigationController?.navigationBar.isHidden = true
        //self.navigationItem.backButtonTitle = "hohoho"
        self.navigationItem.leftBarButtonItem = nil
        self.navigationItem.hidesBackButton = true
        //UINavigationBar.appearance().isHidden = true
      
        //navigationItem.backBarButtonItem =  UIBarButtonItem(title: "Home/Return or nohing", style: .bordered, target: nil, action: nil)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationItem.setHidesBackButton(true, animated: true)
        self.navigationController?.navigationBar.isHidden = true
        //self.navigationItem.backButtonTitle = "hohoho"
        self.navigationItem.leftBarButtonItem = nil
        self.navigationItem.hidesBackButton = true
        //UINavigationBar.appearance().isHidden = true
      
        //navigationItem.backBarButtonItem =  UIBarButtonItem(title: "Home/Return or nohing", style: .bordered, target: nil, action: nil)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        self.navigationItem.setHidesBackButton(true, animated: true)
        self.navigationController?.navigationBar.isHidden = true
        //self.navigationItem.backButtonTitle = "hohoho"
        self.navigationItem.leftBarButtonItem = nil
        self.navigationItem.hidesBackButton = true
        //UINavigationBar.appearance().isHidden = true
      
        //navigationItem.backBarButtonItem =  UIBarButtonItem(title: "Home/Return or nohing", style: .bordered, target: nil, action: nil)
    }
}

and AS you can see in the above code I tried every single way with no change - back button still appear - then I try to make simple change like change the text of the back button or the shape and also there is no result!!

Am I do something wrong :( Because I feel like the whole class is not active for my view

Do I need to create an object of ViewController or something like that? Because I just wrote the mentioned code about my view code.

MY GOAL: I just want to move from view to another with no back button if there is another way I wouldn't mind to do it.

PPLLLSSSS HELPP ME Guys I'm so tired, I'll work on another things until find a solution for that and I'm sure there is a lot of people who want a solution for that issue.

Once I find the solution I'll share it with you guys :) Best Wishes and Regards

Mux02
  • 1
  • 1
  • override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.navigationItem.setHidesBackButton(true, animated: false) } put this code in the second view controller to which you segue into – vrao Jul 06 '22 at 05:12
  • @vrao I will try it again using your code – Mux02 Jul 08 '22 at 10:06

3 Answers3

2

I had the exact same problem and the only solution that worked was adding .navigationBarBackButtonHidden(true) in my SwiftUI view

0

You just have to add the below code in the ViewController where you want to hide the backbutton.

navigationItem.setHidesBackButton(true, animated: true)
Deepa Bhat
  • 174
  • 1
  • 10
  • didn't work :( for me, but I really appreciate your effort and thank you, I think that I'm using it in a wrong way? may be this is the reason but IT TOOK me hours and hours without even changing anything in the nav bar :( – Mux02 Jul 03 '22 at 11:10
  • class ViewController: UIViewController { @IBAction func btnPressed(_ sender: Any) { performSegue(withIdentifier: "testSegue", sender: self)} override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "testSegue"{ let viewController:ViewController2 = segue.destination as! ViewController2 }}} – vrao Jul 10 '22 at 03:19
  • class ViewController2: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.navigationItem.setHidesBackButton(true, animated: false) } – vrao Jul 10 '22 at 03:21
  • I still dont understand why you would want to hide the back button? – vrao Jul 10 '22 at 03:24
0

enter image description here

Segue from viewController to viewController2 and name the segue testSegue. This should work.

vrao
  • 545
  • 2
  • 12
  • 33