Minimal Reproducible Example
class AViewController: UIViewController {
private lazy var someButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("GOGO", for: .normal)
button.addTarget(self, action: #selector(didTapSomeButton), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
view.addSubview(someButton)
someButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
someButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
someButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// tabBarController?.setTabBarHidden(false, animated: true)
}
@objc private func didTapSomeButton() {
let bvc = BViewController()
bvc.hidesBottomBarWhenPushed = true
navigationController?.hidesBarsOnSwipe = true
navigationController?.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(bvc, animated: true)
}
}
class BViewController: UIViewController {
private lazy var someButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("This is B View Controller", for: .normal)
button.addTarget(self, action: #selector(didTapSomeButton), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
view.addSubview(someButton)
someButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
someButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
someButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// tabBarController?.setTabBarHidden(true, animated: true)
}
@objc private func didTapSomeButton() {
print("GOGO")
}
}
// MARK: - Custom UITabBarController
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let aVC = AViewController()
tabs.append(configTab(aVC, title: "Chat", imageName: "message", identifier: "chats", badgeValue: "3"))
tabs.append(configTab(UIViewController(), title: "Contacts", imageName: "person.2", identifier: "contacts"))
tabs.append(configTab(UIViewController(), title: "Discover", imageName: "safari", identifier: "discover"))
tabs.append(configTab(UIViewController(), title: "Me", imageName: "person", identifier: "me"))
tabs.append(configSearchTab(UIViewController(), title: "Search"))
selectedTab = tabs.last
self.tabBarMinimizeBehavior = .onScrollDown
self.bottomAccessory = UITabAccessory(contentView: UIToolbar())
}
// MARK: Configure UITab
func configTab(_ viewController: UIViewController,
title: String,
imageName: String,
identifier: String,
badgeValue: String? = nil) -> UITab {
let tab = UITab(title: title, image: UIImage(systemName: imageName), identifier: identifier) { tab in
tab.badgeValue = badgeValue
tab.userInfo = identifier
return self.configViewController(viewController: viewController, title: title)
}
return tab
}
// MARK: Configure UISearchTab
func configSearchTab(_ viewController: UIViewController, title: String) -> UISearchTab {
// UISearchTab - separated from TabBar and displayed independently
let searchTab = UISearchTab { tab in
viewController.view.backgroundColor = .init(red: .random(in: 0 ... 1),
green: .random(in: 0 ... 1),
blue: .random(in: 0 ... 1),
alpha: 1.0)
return self.configViewController(viewController: viewController, title: title)
}
return searchTab
}
// MARK: Configure UIViewController
func configViewController(viewController: UIViewController, title: String) -> UINavigationController {
let navigationController = UINavigationController(rootViewController: viewController)
viewController.navigationItem.title = title
return navigationController
}
}