ios自定义标签控制器

来源:互联网 发布:区块链技术 知乎 编辑:程序博客网 时间:2024/05/22 14:48
//// MyTabBarViewController.swift// DeepBreathDemo2//// Created by zhengbing on 6/8/16.// Copyright © 2016 zhengbing. All rights reserved.//import UIKitclass 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.实例化buttonlet 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 = iif i == 1000 {btn .selected = true}self.myTabBarView!.addSubview(btn)}configTabBar()}func configTabBar(){// 1 初始化所有的 controllerslet 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 的根视图为 UINavagationViewControllervar 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 = navsself .selectedIndex = 0// self .view.backgroundColor = UIColor.whiteColor()}// 3. 按钮事件func action_BtnClick(sender:UIButton) {// 1.改变被选择的 indexself .selectedIndex = sender.tag - 1000// 2.改变 button 背景sender.selected = truefor i in 1000...1005 {let btn : UIButton = self .myTabBarView! .viewWithTag(i) as! UIButtonif 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 navigationoverride func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {// Get the new view controller using segue.destinationViewController.// Pass the selected object to the new view controller.}*/}
0 0
原创粉丝点击