I use a custom tabBar
in my app. I hides default bar view and create a new view with rounded corners. In some view controllers I want to hide my tabBar
. I use this code to do it:
self.tabBarController!.tabBar.isHidden = true
And my tabBar
is hidden, but the custom view called tabBarView
inside the TabBarViewControler
is not hidden. How to fix this?
code in TabBarViewControler
class:
class TabBarViewController: UITabBarController {
let tabBarView = UIView()
let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .systemChromeMaterialLight))
override func viewDidLoad() {
super.viewDidLoad()
generateTabBar()
setupView()
setupTabBarAppearance()
}
func setupView() {
view.addSubview(tabBarView)
tabBarView.addSubview(blurView)
view.bringSubviewToFront(tabBar)
tabBarView.bringSubviewToFront(blurView)
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
guard let barItemView = item.value(forKey: "view") as? UIView else { return }
let timeInterval: TimeInterval = 0.38
let propertyAnimator = UIViewPropertyAnimator(duration: timeInterval, dampingRatio: 0.9) {
barItemView.transform = CGAffineTransform.identity.scaledBy(x: 0.9, y: 0.9)
}
propertyAnimator.addAnimations({ barItemView.transform = .identity }, delayFactor: CGFloat(timeInterval))
propertyAnimator.startAnimation()
}
func generateTabBar() {
let tableViewController = TableViewController()
let favoritesController = FavoritesController()
let searchViewController = SearchViewController()
let settingsViewController = SettingsViewController()
let vc1 = UINavigationController(rootViewController: tableViewController)
vc1.tabBarItem.title = tableViewController.title
vc1.tabBarItem.image = tableViewController.tabBarImage
let vc2 = UINavigationController(rootViewController: favoritesController)
vc2.tabBarItem.title = favoritesController.title
vc2.tabBarItem.image = favoritesController.tabBarImage
let vc3 = UINavigationController(rootViewController: searchViewController)
vc3.tabBarItem.title = searchViewController.title
vc3.tabBarItem.image = searchViewController.tabBarImage
let vc4 = UINavigationController(rootViewController: settingsViewController)
vc4.tabBarItem.title = settingsViewController.title
vc4.tabBarItem.image = settingsViewController.tabBarImage
viewControllers = [vc1, vc2, vc3, vc4]
}
private func setupTabBarAppearance(){
tabBar.backgroundImage = UIImage()
tabBar.backgroundColor = .clear
tabBar.shadowImage = UIImage()
tabBar.itemPositioning = .centered
tabBar.tintColor = mainColor
tabBar.itemSpacing = 5
tabBarView.backgroundColor = .white
tabBarView.translatesAutoresizingMaskIntoConstraints = false
tabBarView.layer.shadowColor = UIColor.black.withAlphaComponent(1.0).cgColor
tabBarView.layer.shadowOffset = CGSize(width: 0, height: 10)
tabBarView.layer.shadowOpacity = 0.2
tabBarView.layer.shadowRadius = 20
tabBarView.layer.cornerRadius = (tabBar.frame.size.height+20)/2
tabBarView.heightAnchor.constraint(equalToConstant: tabBar.frame.size.height+20).isActive = true
tabBarView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -SafeArea().bottom()).isActive = true
tabBarView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: SafeArea().left()+40).isActive = true
tabBarView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -SafeArea().right()-40).isActive = true
blurView.translatesAutoresizingMaskIntoConstraints = false
blurView.clipsToBounds = true
blurView.layer.cornerRadius = (tabBar.frame.size.height+20)/2
blurView.topAnchor.constraint(equalTo: tabBarView.topAnchor, constant: 0).isActive = true
blurView.bottomAnchor.constraint(equalTo: tabBarView.bottomAnchor, constant: 0).isActive = true
blurView.leadingAnchor.constraint(equalTo: tabBarView.leadingAnchor, constant: 0).isActive = true
blurView.trailingAnchor.constraint(equalTo: tabBarView.trailingAnchor, constant: 0).isActive = true
}
override func viewDidLayoutSubviews() {
tabBar.invalidateIntrinsicContentSize()
tabBar.frame.origin.y = tabBar.frame.origin.y - SafeArea().bottom() - 10
}
}
1
What about if you create a function to hide inside of your TabBarViewController
to hide the view where you are adding the subviews.