今天要講的是Layout的部分,昨天忘記講到,我會用「MainViewController」去把五個畫面涵蓋在裏面,所以如果是昨天的流程,你在建立五個VC的時候應該還要在建立一個叫做MainViewController,我們會需要MainViewController來當五個畫面的「底」,因為我上篇有說,我們是透過下面的TabBar來做換頁,所以TabBar一定要出現在五個畫面當中,但會有個問題,就是每個畫面如果都做一個TabBar然後像昨天那頁設定,太累了QQ,因此這就是今天的重點!
以下是有包含換頁功能的MainViewController:
//
// MainViewController.swift
// FoodDemo
//
// Created by Wang Allen on 2023/9/15.
//
import UIKit
class MainViewController: BaseViewController {
// MARK: - IBOutlet
@IBOutlet weak var container: UIView!
@IBOutlet weak var CustomTabBar: CustomTabBar!
// MARK: - Variables
private var homeViewController = HomeViewController()
private var findingViewController = FindingViewController()
private var walletViewController = WalletViewController()
private var newsViewController = NewsViewController()
private var memberShipViewController = MemberShipViewController()
var vc: [UIViewController] = []
var nowVC: Int = BottomItems.HomeViewController.rawValue
// MARK: - LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
vc = [homeViewController, findingViewController, walletViewController, newsViewController, memberShipViewController]
// updateView(0)
setupNavigationBarStyle(backgroundColor: .black)
let bell = UIBarButtonItem.addButton(self, action: #selector(popMenuView), imageName: "bell",tintColor: .lightGray)
let threeLine = UIBarButtonItem.addButton(self, action: #selector(popMenuView), imageName: "menu",tintColor: .lightGray)
self.navigationItem.rightBarButtonItems = [threeLine, bell]
CustomTabBar.buttonTapped = {
let page = $0
if $0 != self.nowVC {
self.pageChange(page: $0)
if page == BottomItems.HomeViewController.rawValue {
self.navigationItem.rightBarButtonItems = [threeLine, bell]
} else if page == BottomItems.FindingViewController.rawValue {
self.navigationItem.rightBarButtonItems = [threeLine, bell]
} else if page == BottomItems.WalletViewController.rawValue {
self.navigationItem.rightBarButtonItems = []
} else if page == BottomItems.NewsViewController.rawValue {
self.navigationItem.rightBarButtonItems = []
} else if page == BottomItems.MemberShipViewController.rawValue {
self.navigationItem.rightBarButtonItems = []
}
}
}
}
// MARK: - UI Settings
@objc func popMenuView() {
let nextVC = NotificationCenterViewController()
pushViewController(nextVC, animated: true) {
print("Enter the Notification Center")
}
}
func pageChange(page: Int) {
self.updateView(page)
}
private func updateView(_ index: Int) {
nowVC = index
if children.first(where: { String(describing: $0.classForCoder) == String(describing: vc[index].classForCoder) }) == nil {
addChild(vc[index])
vc[index].view.frame = container.bounds
}
container.addSubview(vc[index].view ?? UIView())
let lbNavTitle = UILabel (frame: CGRect(x: 20, y: 10, width: 320, height: 120))
lbNavTitle.center = CGPoint(x: 50, y: 0)
lbNavTitle.text = CustomTabBar.items[index].title
lbNavTitle.font = UIFont.systemFont(ofSize: 20)
lbNavTitle.backgroundColor = UIColor.black
lbNavTitle.textColor = UIColor.lightGray
lbNavTitle.numberOfLines = 0
lbNavTitle.textAlignment = .left
}
}
圖包的部分可以去這個網站抓,他是免費商用的:
https://www.iconshock.com/freeicons
成果如下: