ios自定义标签控制器
2016-06-14 22:04
381 查看
// // MyTabBarViewController.swift // DeepBreathDemo2 // // Created by zhengbing on 6/8/16. // Copyright © 2016 zhengbing. All rights reserved. // import UIKit class 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.实例化button let 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 = i if i == 1000 { btn .selected = true } self.myTabBarView!.addSubview(btn) } configTabBar() } func configTabBar(){ // 1 初始化所有的 controllers let 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 的根视图为 UINavagationViewController var 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 = navs self .selectedIndex = 0 // self .view.backgroundColor = UIColor.whiteColor() } // 3. 按钮事件 func action_BtnClick(sender:UIButton) { // 1.改变被选择的 index self .selectedIndex = sender.tag - 1000 // 2.改变 button 背景 sender.selected = true for i in 1000...1005 { let btn : UIButton = self .myTabBarView! .viewWithTag(i) as! UIButton if 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 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. } */ }
相关文章推荐
- [置顶] iOS开发 GET、POST请求方法:NSURLSession篇
- ios属性修饰符详解
- 【转】iOS学习之容易造成循环引用的三种场景
- iOS开发--二维码的扫描
- iOS与HTML5交互方法总结
- iOS获取手势的tag
- iOS开发 GET、POST请求方法:NSURLConnection篇
- opencv-ios开发笔记8 从沙盒中找到训练生成的特征文件
- iOS开发--二维码的生成
- iOS 小知识 - #if , #ifdef , #ifndef.
- [置顶] 关于iOS socket都在这里了
- [置顶] iOS开发—XML/JSON数据解析
- NSTimer定时器
- iOS真机调试配置
- iOS开发-基于原生JS与OC方法互相调用并传值(附HTML代码)
- 自动计算cell高度
- TableView优化之高度缓存
- iOS 强制竖屏或是横屏 (代码设置后仍无效的问题)
- IOS通知和代理的用法以及优劣比较
- ios开发中的runloop