界面API实现tabBar红点的显示和隐藏、tarBar文本的显示和隐藏、tabBar的显示和隐藏、动态改变tanBar的图标和文字 完整代码
时间: 2024-03-12 11:50:18 浏览: 147
以下是实现上述功能的完整代码:
```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"))
```
阅读全文