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

ios-swift 自定义TabBarViewController

2016-06-11 21:52 375 查看
//

// 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.

}

*/

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