您的位置:首页 > 移动开发 > Swift

Swift ☞ 自定义UITabBarController

2016-06-29 22:30 651 查看
上代码咯。。。。

原文链接:http://blog.csdn.net/magiczyj/article/details/51786399

import UIKit

class CustomTabBarBtn: UIButton {

var mark:NSInteger = 0;

}

let SCREEN_W = UIScreen.mainScreen().bounds.size.width;
let SCREEN_H = UIScreen.mainScreen().bounds.size.height;
let TABBAR_H:CGFloat = 49.0;
let TABBAR_BTN_W = SCREEN_W / 5.0;
class MainViewController: UITabBarController {
let backGroundImgV = UIImageView();

override func viewDidLoad() {
super.viewDidLoad()
foreDeclare();
creatTabBar();
}

private func foreDeclare() {

tabBar.hidden = true;
backGroundImgV.userInteractionEnabled = true;
}

func creatTabBar() {

backGroundImgV.image = UIImage(named: "backgroundimage.png");
backGroundImgV.frame = CGRect(x: 0, y: SCREEN_H - TABBAR_H, width: SCREEN_W, height: TABBAR_H);
view.addSubview(backGroundImgV);

for i in 0...4 {

let tabBarButton = CustomTabBarBtn();
tabBarButton.frame = CGRect(x: TABBAR_BTN_W * CGFloat(i), y: 0, width: TABBAR_BTN_W, height: TABBAR_H);
tabBarButton.setBackgroundImage(UIImage(named: "tabbar_\(i)"), forState: .Normal);
tabBarButton.setBackgroundImage(UIImage(named: "tabbar_selected_\(i)"), forState: .Selected);
tabBarButton.addTarget(self, action: "itemClick:", forControlEvents: .TouchUpInside);
tabBarButton.mark = i;
if i == 1 {
tabBarButton.selected = true;
}
backGroundImgV.addSubview(tabBarButton);

let vc = UIViewController();
vc.title = "页面\(i)";
let nav = UINavigationController(rootViewController: vc);
addChildViewController(nav);
}

}

func itemClick(btn: CustomTabBarBtn) {
selectedIndex = btn.mark;
for tabbarBtn in backGroundImgV.subviews {
let item = tabbarBtn as! CustomTabBarBtn;
if item.mark == btn.mark {
item.selected = true;
}else {
item.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.
}
*/

}


效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息