// MyTabBarViewController.swift
// DeepBreathDemo2
//
// Created by zhengbing on 6/8/16.
// Copyright © 2016 zhengbing. All rights reserved.
//
import UIKit
class MyTabBarViewController: UITabBarController {
var myTabBarView : UIView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// 比例化(宽:高 = 1.28 :1)计算 TabBar 高度,适配各种机型
let btnW: CGFloat = Tools .kWidth/5
// 1. 自定义TabBar的背景视图
self.myTabBarView = ({
let v = UIView (frame: CGRect(x: 0, y: CGRectGetHeight(self.view.bounds)
- btnW/1.28, width: CGRectGetWidth(self.view.bounds), height:
btnW/1.28))
v.backgroundColor = UIColor .clearColor()
return v
})()
self.view.addSubview(self.myTabBarView!)
// 2. 按钮
for i in 1000...1005 {
// 1.实例化button
let btn = UIButton(frame: CGRect(x: btnW * CGFloat(i-1000), y: 0, width: btnW, height: btnW/1.28))
// 2.定义button背景图片
let normalImageName : String = String(i) .stringByAppendingString("-0.png")
let selectedImageName : String = String(i) .stringByAppendingString("-1.png")
btn .setBackgroundImage(UIImage(named: normalImageName), forState: UIControlState .Normal)
btn .setBackgroundImage(UIImage(named: selectedImageName), forState: UIControlState .Selected)
// 3.给button添加点击方法
btn .addTarget(self, action: #selector(action_BtnClick(_:)), forControlEvents: UIControlEvents .TouchUpInside)
// 默认第一个被选中
btn.tag = i
if i == 1000 {
btn .selected = true
}
self.myTabBarView!.addSubview(btn)
}
configTabBar()
}
func configTabBar(){
// 1 初始化所有的 controllers
let Tab0 = Tab0ViewController()
let Tab1 = Tab1ViewController()
let Tab2 = Tab2ViewController()
let Tab3 = Tab3ViewController()
let Tab4 = Tab4ViewController()
// 2.把 controller 用数组装起来
let ctlNames = [Tab0, Tab1, Tab2, Tab3, Tab4]
// 3.组装 title 数组
let ctlTitiles = ["Tab0", "Tab1", "Tab2", "Tab3", "Tab4"]
// 4.viewController 的根视图为 UINavagationViewController
var navs = [UINavigationController]()
for (idx, vc) in ctlNames.enumerate() {
vc.title = ctlTitiles[idx]
vc.view.backgroundColor = UIColor.whiteColor()
let nav = UINavigationController(rootViewController: vc)
nav.navigationBar.tintColor = UIColor.whiteColor()
nav.navigationBar.titleTextAttributes =
[NSFontAttributeName:UIFont.boldSystemFontOfSize(22),
NSForegroundColorAttributeName:UIColor.blackColor()]
navs.append(nav)
}
// 5.设置索引视图
self .viewControllers = navs
self .selectedIndex = 0
// self .view.backgroundColor = UIColor.whiteColor()
}
// 3. 按钮事件
func action_BtnClick(sender:UIButton) {
// 1.改变被选择的 index
self .selectedIndex = sender.tag - 1000
// 2.改变 button 背景
sender.selected = true
for i in 1000...1005 {
let btn : UIButton = self .myTabBarView! .viewWithTag(i) as! UIButton
if i != sender.tag {
btn.selected = false
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
自定义tabbar,tabbar整体悬浮状态,效果如下图:
实现步骤:
一. 自定义TabBar
2.重新计算UITabBarButton的frame,中间的Button要区别于其余的Button,他的高度要覆盖到中间的图片
二. 自定义UITabBarController
先来判断机型,不同机型底部高度不同,然后在viewDidLayoutSubviews()方法中重新设置tabbar的高度,在addChildViewControllers()方法中设置对应的Controller,title和图片,在setChildViewController方法中设置NavigationController
三. 使用
在SceneDelegate文件中调用即可
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)