隐藏UITabBar

self.tabBarController.tabBar.hidden == YES

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现上述功能的完整代码: ```swift import UIKit class ViewController: UIViewController { // MARK: - Properties private var tabBar: UITabBar! private var tabBarItems: [UITabBarItem] = [] // MARK: - View Lifecycle override func viewDidLoad() { super.viewDidLoad() // Create tab bar createTabBar() // Add view controllers to tab bar let vc1 = UIViewController() vc1.tabBarItem = UITabBarItem(title: "Item 1", image: UIImage(named: "icon1"), selectedImage: nil) tabBarItems.append(vc1.tabBarItem) let vc2 = UIViewController() vc2.tabBarItem = UITabBarItem(title: "Item 2", image: UIImage(named: "icon2"), selectedImage: nil) tabBarItems.append(vc2.tabBarItem) let vc3 = UIViewController() vc3.tabBarItem = UITabBarItem(title: "Item 3", image: UIImage(named: "icon3"), selectedImage: nil) tabBarItems.append(vc3.tabBarItem) let vc4 = UIViewController() vc4.tabBarItem = UITabBarItem(title: "Item 4", image: UIImage(named: "icon4"), selectedImage: nil) tabBarItems.append(vc4.tabBarItem) let vc5 = UIViewController() vc5.tabBarItem = UITabBarItem(title: "Item 5", image: UIImage(named: "icon5"), selectedImage: nil) tabBarItems.append(vc5.tabBarItem) let viewControllers = [vc1, vc2, vc3, vc4, vc5] self.viewControllers = viewControllers // Set initial selected tab self.selectedIndex = 0 // Show red dot on tab bar item 3 showRedDot(on: 2) // Hide tab bar item 4 text hideTabBarItemText(for: 3) } // MARK: - Tab Bar Helpers private func createTabBar() { tabBar = UITabBar() tabBar.translatesAutoresizingMaskIntoConstraints = false view.addSubview(tabBar) NSLayoutConstraint.activate([ tabBar.leadingAnchor.constraint(equalTo: view.leadingAnchor), tabBar.trailingAnchor.constraint(equalTo: view.trailingAnchor), tabBar.bottomAnchor.constraint(equalTo: view.bottomAnchor), tabBar.heightAnchor.constraint(equalToConstant: 50) ]) tabBar.delegate = self } private func showTabBar() { UIView.animate(withDuration: 0.3) { self.tabBar.frame.origin.y = self.view.frame.height - self.tabBar.frame.height } } private func hideTabBar() { UIView.animate(withDuration: 0.3) { self.tabBar.frame.origin.y = self.view.frame.height } } private func showRedDot(on index: Int) { guard index < tabBarItems.count else { return } let item = tabBarItems[index] item.badgeValue = " " } private func hideRedDot(from index: Int) { guard index < tabBarItems.count else { return } let item = tabBarItems[index] item.badgeValue = nil } private func hideTabBarItemText(for index: Int) { guard index < tabBarItems.count else { return } let item = tabBarItems[index] item.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .normal) item.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .selected) } private func showTabBarItemText(for index: Int) { guard index < tabBarItems.count else { return } let item = tabBarItems[index] item.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal) item.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .selected) } private func updateTabBarItem(at index: Int, with title: String? = nil, image: UIImage? = nil) { guard index < tabBarItems.count else { return } let item = tabBarItems[index] if let title = title { item.title = title } if let image = image { item.image = image } } } // MARK: - UITabBarDelegate extension ViewController: UITabBarDelegate { func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { // Handle tab bar item selection } } ``` 使用时,只需要调用相应的方法即可实现对应的功能。例如,要在第三个 tab bar item 上显示红点,可以调用: ```swift showRedDot(on: 2) ``` 要隐藏第四个 tab bar item 的文本,可以调用: ```swift hideTabBarItemText(for: 3) ``` 要动态改变第二个 tab bar item 的图标和文字,可以调用: ```swift updateTabBarItem(at: 1, with: "New Title", image: UIImage(named: "new_icon")) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值