Class Tabbarviewcontroller:uitabbarcontroller
{
var tabbarbgimg:uiimageview?
var tabbarbgimgselected:uiimageview?
var Selectedbutton:uibutton? Mark the selected button
Override Func Viewdidload ()
{
Super.viewdidload ()
Self.view.backgroundColor = Uicolor.lightgraycolor ()
Remove System Tabbar, add custom Tabbar
Self.tabBar.removeFromSuperview ()
var Customtabbar = UIView (frame:self.tabBar.frame)
Customtabbar.backgroundcolor = Uicolor.clearcolor ()
Self.view.addSubview (Customtabbar)
Add two controllers to Tabbar
Self.viewcontrollers = [Uinavigationcontroller (Rootviewcontroller:homeviewcontroller ()), UINavigationController ( Rootviewcontroller:gameviewcontroller ())]
Add a button to a custom Tabbar
var btnscount:int = 2
For temp in 0...1
{
var btnh:cgfloat = 60
var btnw:cgfloat = Uiscreen.mainscreen (). Bounds.size.width/cgfloat (Btnscount)
var btnx:cgfloat = cgfloat (temp) * BTNW
var btny:cgfloat = 0
var btn = Pptabbarbutton (Frame:cgrectmake (btnx, Btny, BTNW, BTNH))
Customtabbar.addsubview (BTN)
Btn.tag = Temp
Btn.addtarget (Self, Action: "Didclickbutton:", ForControlEvents:UIControlEvents.TouchUpInside)
Btn.setbackgroundimage (UIImage (named: "Frame-new&hot-top-normal"), ForState:UIControlState.Normal)
Btn.setbackgroundimage (UIImage (named: "frame-new&hot-top-pressed"), forState:UIControlState.Selected)
If temp = = 0
{
Btn.selected = True
Self.selectedbutton = btn
}
Set Button background picture
If temp = = 0
{
Btn.setimage (UIImage (named: "Tab-video-line"), ForState:UIControlState.Normal)
Btn.setimage (UIImage (named: "Tab-video-solid"), forState:UIControlState.Selected)
}
else if temp = = 1
{
Btn.setimage (UIImage (named: "Tab-game-line"), ForState:UIControlState.Normal)
Btn.setimage (UIImage (named: "Tab-game-solid"), forState:UIControlState.Selected)
}
}
}
Func Didclickbutton (Button:uibutton), Void
{
Self.selectedbutton?. Selected = False
Button.selected = True
Self.selectedbutton = button
Self.selectedindex = Button.tag
}
}
Swift Custom Tabbar