Swift完全自定義Tabbar

先創(chuàng)建CusTomTabBar.swift文件,之后代碼如下:

//tabbar高度

let tabBarViewHeight: CGFloat = 60

//tabbar顏色

let tabBarBackColor:UIColor = UIColor.black

//用來(lái)裝imageView的數(shù)組

var tabImgViews = [UIImageView]()

var imgArr = ["homePage", "mall" ,"personal"]

var imgSelArr = ["homePage_Se", "mall_Se", "personal_Se"]

var barTitleArr = ["首頁(yè)","商城","個(gè)人"]

var tabBarView = UIView()

override func viewDidLoad()

{

super.viewDidLoad()

self.view.backgroundColor = UIColor.white

self.tabBar.isHidden = true

//初始化控制器加入viewControllers

initControllers()

//自定義tabbar

customTabBar()

}

func customTabBar() -> Void

{

tabBarView = UIView(frame: CGRect(x: 0, y: SCREEN_HEIGHT - tabBarViewHeight, width: SCREEN_WIDTH, height: tabBarViewHeight))

tabBarView.backgroundColor = tabBarBackColor

self.view.addSubview(tabBarView)

for index in 0..<imgArr.count

{

let btn = UIButton(frame: CGRect(x:(CGFloat(index)*SCREEN_WIDTH/3),y: (CGFloat)(5), width:SCREEN_WIDTH/3, height:45))

let label = UILabel(frame: CGRect(x: SCREEN_WIDTH/8 - 35, y: 12, width: 100, height: 50))

label.text = barTitleArr[index]

label.textAlignment = .center

label.tag = 200 + index

label.font = UIFont.systemFont(ofSize:11)

label.textColor = UIColor.white

btn.addSubview(label)

let tabImg = UIImageView(frame: CGRect(x: SCREEN_WIDTH/8 , y: 0, width: 30, height: 30))

tabImg.image = UIImage(named: imgArr[index])

tabImg.tag = 300 + index

btn.addSubview(tabImg)

if (index==0) {

tabImg.image =  UIImage(named:imgSelArr[index])

}

btn.tag = index + 100

btn.addTarget(self, action:#selector(tabAction), for:.touchUpInside)

tabBarView.addSubview(btn)

btn.bringSubview(toFront: btn)

tabImgViews.append(tabImg)

}

}

/*

點(diǎn)擊tabbarItem操作

*/

func tabAction(obj: UIButton) -> Void

{

let indexSel = obj.tag - 100

//對(duì)應(yīng)選中的控制器

self.selectedIndex = indexSel

for index in 0..<tabImgViews.count

{

if(index == indexSel)

{

tabImgViews[indexSel].image = UIImage(named: imgSelArr[indexSel])

}else{

tabImgViews[index].image = UIImage(named: imgArr[indexSel])

}

}

}

func initControllers() -> Void

{

let firstTabView = UIStoryboard.init(name:"FirstPage" , bundle: nil).instantiateViewController(withIdentifier: "FirstPageViewController")

let secondTabView = UIStoryboard.init(name:"SecondPage" , bundle: nil).instantiateViewController(withIdentifier: "SecondPageViewController")

let thirdTabView = UIStoryboard.init(name:"ThirdPage" , bundle: nil).instantiateViewController(withIdentifier: "ThirdPageViewController")

var viewArr = [firstTabView, secondTabView, thirdTabView]

var viewCtlArr = [AnyObject]()

for index in 0..<viewArr.count

{

if(index != 2)

{

let navController = UINavigationController(rootViewController: viewArr[index])

viewCtlArr.append(navController)

}else{

viewCtlArr.append(viewArr[index])

}

}

self.viewControllers = (viewCtlArr as! [UIViewController])

}

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,320評(píng)論 4 61
  • 概念:以終為始 想象一下,你受命去蓋一棟大樓。你怎么開(kāi)始?兄弟們,跟我上!干起來(lái)再說(shuō)!這顯然是不行的。估計(jì)你這樣的...
    洋_蔥頭閱讀 464評(píng)論 0 0
  • 在夏至日這天定下2017需要完成的目標(biāo):1.能用python爬網(wǎng)2.熟練掌握git命令及相關(guān)操作3.古琴學(xué)會(huì)關(guān)山月...
    BonaTang閱讀 107評(píng)論 0 0
  • 重新聽(tīng)了弘丹老師的課程,先一步步來(lái),從如何寫(xiě)好一個(gè)標(biāo)題開(kāi)始。首先,從整理筆記開(kāi)始。 1. 標(biāo)題的作用? 1)吸引注...
    在事上磨閱讀 224評(píng)論 0 0

友情鏈接更多精彩內(nèi)容