简体   繁体   中英

How to show back button in a tab bar viewcontroller?

I have a navigation controller with a home view controller on its root. Then I push a tab bar view controller. The back button disappears. How can I return from the tab bar view controller to the home view controller via back button? How can I make it visible again?

I have tried:

let navItem = self.navigationController?.navigationItem
let navItem2 = self.navigationItem;
leftBarButton = UIBarButtonItem()
leftBarButton.image = UIImage(named: "arrows-back-icon-24.png")
leftBarButton.action = #selector(self.popViewController);
leftBarButton.target = self
navItem?.leftBarButtonItem = leftBarButton
navItem2.leftBarButtonItem = leftBarButton;

I also have tried:

let navItem = self.navigationController?.navigationItem
let navItem2 = self.navigationItem;
navItem?.leftBarButtonItem = nil;
navItem2.leftBarButtonItem = nil;

All is not working. Please help. Thanks.

Try this: Assign UITabBarController class file to Tab Bar Controller, just like view controller.

import UIKit
//this is TabBarController.swift file
class TabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

在此处输入图片说明

And push it from the HomeVC like this (here, I am using UIButton for Push):

@IBAction func btnPush(_ sender: UIButton) {
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "TabBarController") as! TabBarController
    self.navigationController?.pushViewController(vc, animated: true)
}

I had the same issue, but in my case, it was due to the viewWillAppear method. Try adding the following to each of the UIViewControllers embedded in the corresponding UITabBarController:

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }

Hope this works for you.

let btn1 = UIButton(type: .custom)
btn1.setImage(UIImage(named: "image"), for: .normal)
btn1.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
btn1.addTarget(self, action: #selector(methodname), for: .touchUpInside)
let item1 = UIBarButtonItem(customView: btn1)

let btn2 = UIButton(type: .custom)
btn2.setImage(UIImage(named: "image"), for: .normal)
btn2.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
btn2.addTarget(self, action: #selector(methodName), for: .touchUpInside)
let item2 = UIBarButtonItem(customView: btn2)  

self.navigationItem.setLeftBarButtonItems([item1,item2], animated: true)

Try this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM