1.TabBarView
class XTKMainTabBarController: UITabBarController {
var senderBtn = MainTabBtn()//选中的分类按钮
var buttonsArray = [UIButton]()//按钮数组
var namesArray = [[String: String]]()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.brown
// 1. 加载配置数据
namesData()
//2.初始化子试图控制器
setupSubControllers()
//3.添加分类按钮
addItems()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var tabFrame = tabBar.frame
tabFrame.size.height = kTabBarHeight
tabFrame.origin.y = self.view.frame.size.height - kTabBarHeight
self.tabBar.frame = tabFrame
}
//MARK: 懒加载
//主视图
private lazy var mainView : UIView = {
let mainView = UIView(frame: CGRect(x: 0, y: 0, width: KSCREEN_WIDTH, height: kTabBarHeight))
let lineView = UIView(frame: CGRect(x: 0, y: 0, width: KSCREEN_WIDTH, height: 1))
mainView.backgroundColor = UIColor.brown
mainView.addSubview(lineView)
return mainView
}()
}
extension XTKMainTabBarController {
private func namesData() {
namesArray = [
["title" : "首页", "normalImage" : "tabbarNew_0", "selectedImage" : "tabbarNew_0"],
["title" : "首页1", "normalImage" : "tabbarNew_1", "selectedImage" : "tabbarNew_1"],
["title" : "首页2", "normalImage" : "tabbarNew_2", "selectedImage" : "tabbarNew_2"],
["title" : "首页3", "normalImage" : "tabbarNew_3", "selectedImage" : "tabbarNew_3"],
["title" : "首页4", "normalImage" : "tabbarNew_4", "selectedImage" : "tabbarNew_4"],
]//子视图属性数组
}
//MARK: 初始化子试图控制器
private func setupSubControllers() {
var controllers = [UIViewController]()
let nav0 = XTKNavigationController(rootViewController: XTKHomeVC.init())
let nav1 = XTKNavigationController(rootViewController: XTKQuoteVC.init())
let nav2 = XTKNavigationController(rootViewController: XTKTradeVC.init())
let nav3 = XTKNavigationController(rootViewController: XTKContractVC.init())
let nav4 = XTKNavigationController(rootViewController: XTKAssetsHomeVC.init())
controllers.append(nav0)
controllers.append(nav1)
controllers.append(nav2)
controllers.append(nav3)
controllers.append(nav4)
viewControllers = controllers
}
//MARK: 添加分类按钮
private func addItems() {
// 1. 创建版视图
self.tabBar.backgroundColor = UIColor.clear
self.tabBar.addSubview(mainView)
// 2. 创建分类按钮
buttonsArray = [UIButton]()
let kBtnW = KSCREEN_WIDTH/CGFloat(namesArray.count)
for index in 0..<namesArray.count {
let mainBtn = MainTabBtn(frame: CGRect(x: CGFloat(index) * kBtnW, y: 0, width: kBtnW, height: 59))
if index == 0 {
mainBtn.isSelected = true
senderBtn = mainBtn
}
mainBtn.setTitleColor(UIColor.darkGray, for: UIControl.State.normal)
mainBtn.setTitleColor(UIColor.blue, for: UIControl.State.selected)
mainBtn.tag = index
mainBtn.addTarget(self, action: #selector(clickMainBtn), for: UIControl.Event.touchUpInside)
mainView.addSubview(mainBtn)
buttonsArray.append(mainBtn)
}
//调用刷新按钮图标
reloadButtonImage()
}
//item按钮点击事件
@objc func clickMainBtn(sender: UIButton) {
// 1. 改变选中状态
senderBtn.isSelected = false;
sender.isSelected = true;
senderBtn = sender as! MainTabBtn;
// 2. 设置选中的索引的控制器
self.selectedIndex = sender.tag;
}
//MARK: 3. 刷新按钮图标(此处后续可根据服务器数据动态刷新)
private func reloadButtonImage() {
var titlesArray = [String]()
titlesArray.append("首页1")
titlesArray.append("首页2")
titlesArray.append("首页3")
titlesArray.append("首页4")
titlesArray.append("首页5")
for index in 0..<buttonsArray.count {
let mainBtn = self.buttonsArray[index]
mainBtn.setTitle(titlesArray[index], for: UIControl.State.normal)
mainBtn.setImage(UIImage(named: "tabbarNew_\(index)"), for: UIControl.State.normal)
mainBtn.setImage(UIImage(named: "tabbarNew_\(index)"), for: UIControl.State.selected)
}
}
///设置想要展现的视图
func setIndex(index:NSInteger) {
self.clickMainBtn(sender: self.buttonsArray[index])
}
}
2.UIButton
class MainTabBtn: UIButton {
let kImageWith: CGFloat = 24
override init(frame: CGRect) {
super.init(frame: frame)
titleLabel?.textAlignment = NSTextAlignment.center
titleLabel?.font = UIFont.systemFont(ofSize: 12)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
imageView?.frame = CGRect(x: (self.frame.size.width - kImageWith)*0.5, y: 7, width: kImageWith, height: kImageWith)
titleLabel?.frame = CGRect(x: 0, y: imageView?.frame.maxY ?? 0, width: self.frame.size.width, height: self.frame.size.height - (imageView?.frame.maxY ?? 0))
}
}
3.NavigationController
class XTKNavigationController: UINavigationController, UIGestureRecognizerDelegate, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// 隐藏系统导航栏
navigationBar.isHidden = true
// 1.如果是因为自定义导航按钮而导致手势返回失效,那么可以在NavigationController的viewDidLoad函数中添加如下代码:
if ((interactivePopGestureRecognizer?.responds(to: Selector(("interactivePopGestureRecognizer")))) != nil){
interactivePopGestureRecognizer?.delegate = self
delegate = self
}
}
//2.跳转自动显示和隐藏TabBar
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
if children.count > 0 {
tabBarController?.tabBar.isHidden = true
viewController.hidesBottomBarWhenPushed = true
}
if ((interactivePopGestureRecognizer?.responds(to: Selector(("interactivePopGestureRecognizer")))) != nil) && animated == true {
interactivePopGestureRecognizer?.isEnabled = false;
}
super.pushViewController(viewController, animated: animated)
}
//3.返回手势
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if (gestureRecognizer == self.interactivePopGestureRecognizer) {
if (viewControllers.count < 2 || self.visibleViewController == self.viewControllers[0]) {
return false;
}
}
return true;
}
//4.这个代理方法的功能是哪个控制器进行展示了,也就是栈顶控制器
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
if ((interactivePopGestureRecognizer?.responds(to: Selector(("interactivePopGestureRecognizer")))) != nil) {
interactivePopGestureRecognizer?.isEnabled = true;
}
}
//5.这个方法看名称可以知道也是出栈,但是他出的非常彻底,直接回到了栈底,把栈里的子控制器都出栈了
override func popToRootViewController(animated: Bool) -> [UIViewController]? {
if ((interactivePopGestureRecognizer?.responds(to: Selector(("interactivePopGestureRecognizer")))) != nil) && animated == true {
interactivePopGestureRecognizer?.isEnabled = false;
}
return super.popToRootViewController(animated: animated)
}
//6.这个方法看多了个参数,也就说可以出栈到指定的控制器那个位置,就是指定的控制器之前的子控制器都得出栈。
override func popToViewController(_ viewController: UIViewController, animated: Bool) -> [UIViewController]? {
if ((interactivePopGestureRecognizer?.responds(to: Selector(("interactivePopGestureRecognizer")))) != nil) && animated == true {
interactivePopGestureRecognizer?.isEnabled = false;
}
return super.popToViewController(viewController, animated: animated)
}
}
网友评论