我在我的应用中使用了导航项。在注销方法中,我已经弹出到根视图控制器,即我的loginViewController,但是在执行此操作时导航栏被删除了。我尝试将代码加载到viewWillAppear中,但仍然无法正常工作。你能帮我吗?
这是我的注销代码:
@IBAction func btnLogOutClicked(_ sender: Any) {
let log : LoginViewController = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
let window:UIWindow = UIApplication.shared.keyWindow!
let navControl : UINavigationController = window.rootViewController as! UINavigationController
if (!(navControl .isEqual(log)))
{
window.rootViewController = log
}
navControl.popToRootViewController(animated: true)
}
我的viewWillAppear-
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.isNavigationBarHidden = false
self.navigationItem.hidesBackButton = true
self.navigationItem.title = "Select Client"
// create a new butto
let button: UIButton = UIButton(type: UIButtonType.custom)
//set image for button
button.setImage(UIImage(named: "logo-icon"), for: UIControlState.normal)
//set frame
button.frame = CGRect(x: 0, y: 0, width: 50, height: 30)
let barButton = UIBarButtonItem(customView: button)
//assign button to navigationbar
self.navigationItem.leftBarButtonItem = barButton
}
这是我的故事板-
我正在使用pushViewController从登录视图控制器访问标签栏控制器
答案 0 :(得分:0)
尝试下面的代码来更改NavigationController
堆栈:
let oldVCs = self.navigationController?.viewControllers
let newVCs = [UIViewController]()
// Add your root VC in new array of VCs
newVCs.append(oldVCs![0])
// Add your new VC just after your root VC
let log : LoginViewController = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
newVCs.append(log)
self.navigationController?.setViewControllers(newVCs , animated: true)
在这种动画中,就像将新控制器推入堆栈一样。
如果要完全离开该堆栈并创建一个新堆栈:
let log : LoginViewController = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
let loginNavigationController = UINavigationController(rootViewController: log)
self.view.window?.rootViewController = loginNavigationController