enum BottomItems: Int, CaseIterable {
case HomeViewController = 0, FindingViewController, WalletViewController, NewsViewController, MemberShipViewController
var title: String {
switch self {
case .HomeViewController:
return "首頁"
case .FindingViewController:
return "尋找餐廳"
case .WalletViewController:
return "錢包"
case .NewsViewController:
return "最新消息"
case .MemberShipViewController:
return "會員中心"
}
}
var imageName: String {
switch self {
case .HomeViewController:
return "home"
case .FindingViewController:
return "search"
case .WalletViewController:
return "wallet-filled-money-tool"
case .NewsViewController:
return "megaphone"
case .MemberShipViewController:
return "user"
}
}
}
//
// ViewController.swift
// FoodDemo
//
// Created by Wang Allen on 2023/9/19.
//
import UIKit
enum BottomItems: Int, CaseIterable {
case HomeViewController = 0, FindingViewController, WalletViewController, NewsViewController, MemberShipViewController
var title: String{
switch self {
case .HomeViewController:
return "首頁"
case .FindingViewController:
return "尋找餐廳"
case .WalletViewController:
return "錢包"
case .NewsViewController:
return "最新消息"
case .MemberShipViewController:
return "會員中心"
}
}
}
class CustomTabBar: UIView {
@IBOutlet weak var homeView: CustomTabBarView!
@IBOutlet weak var findingView: CustomTabBarView!
@IBOutlet weak var walletView: CustomTabBarView!
@IBOutlet weak var newsView: CustomTabBarView!
@IBOutlet weak var membershipView: CustomTabBarView!
var buttonTapped: ((Int) -> ())? = nil
var items = BottomItems.allCases
var orangeColor = UIColor(red: 255/255, green: 137/255, blue: 56/255, alpha: 1)
var cell: [UIImageView] = []
override func awakeFromNib() {
super.awakeFromNib()
addXibView()
self.homeView.viewControllerImg.tintColor = .orange
cell.append(homeView.viewControllerImg)
cell.append(findingView.viewControllerImg)
cell.append(walletView.viewControllerImg)
cell.append(newsView.viewControllerImg)
cell.append(membershipView.viewControllerImg)
}
func changeColor(tag: Int) {
for i in 0 ..< cell.count {
cell[i].tintColor = (i == tag) ? .systemOrange : .lightGray
}
}
}
extension CustomTabBar: CustomViewListener {
func target(stringTag: Int) {
buttonTapped?(items[stringTag].rawValue)
changeColor(tag: stringTag)
}
}
fileprivate extension CustomTabBar {
//加入畫面
func addXibView() {
if let loadView = Bundle(for: CustomTabBar.self).loadNibNamed("CustomTabBar", owner: self, options: nil)?.first as? UIView{
addSubview(loadView)
loadView.frame = bounds
}
homeView.delegate = self
findingView.delegate = self
walletView.delegate = self
newsView.delegate = self
membershipView.delegate = self
homeView.setInit(tag: 0, text: "首頁", image: UIImageView.init(image: UIImage(named: "home")), tinColor: .lightGray)
findingView.setInit(tag: 1, text: "尋找餐廳", image: UIImageView.init(image: UIImage(named: "search")), tinColor: .lightGray)
walletView.setInit(tag: 2, text: "錢包", image: UIImageView.init(image: UIImage(named: "wallet-filled-money-tool")), tinColor: .lightGray)
newsView.setInit(tag: 3, text: "最新消息", image: UIImageView.init(image: UIImage(named: "megaphone")), tinColor: .lightGray)
membershipView.setInit(tag: 4, text: "會員中心", image: UIImageView.init(image: UIImage(named: "user")), tinColor: .lightGray)
}
}