class TabBarViewController: UITabBarController
{
var tabBarBgImg : UIImageView?
var tabBarBgImgSelected : UIImageView?
var selectedButton : UIButton? // 标记选中的按钮
override func viewDidLoad()
{
super.viewDidLoad()
self.view.backgroundColor = UIColor.lightGrayColor()
// 移除系统tabBar,添加自定义tabBar
self.tabBar.removeFromSuperview()
var customTabBar = UIView(frame: self.tabBar.frame)
customTabBar.backgroundColor = UIColor.clearColor()
self.view.addSubview(customTabBar)
// 添加两个控制器到tabBar
self.viewControllers = [UINavigationController(rootViewController: HomeViewController()), UINavigationController(rootViewController: GameViewController())]
// 添加按钮到自定义的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
}
// 设置按钮背景图片
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
}
}