swift 它们的定义TabBarItem

1.效果图

   

2.NewsViewController.swift

//
//  NewsViewController.swift
//  NavigationDemo
//
//  Created by 赵超 on 14-6-27.
//  Copyright (c) 2014年 赵超. All rights reserved.
//

import UIKit

class NewsViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor=UIColor.blueColor()
        self.title="新闻"
    }
}

3.MoviewViewController.swift

//
//  MovieViewController.swift
//  NavigationDemo
//
//  Created by 赵超 on 14-6-27.
//  Copyright (c) 2014年 赵超. All rights reserved.
//

import UIKit

class MovieViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor=UIColor.redColor()
        self.title="电影"
    }
}

4.AppDelegate.swift

//
//  AppDelegate.swift
//  NavigationDemo
//
//  Created by 赵超 on 14-6-27.
//  Copyright (c) 2014年 赵超. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // Override point for customization after application launch.
        self.window!.backgroundColor = UIColor.whiteColor()
        self.window!.makeKeyAndVisible()
        //设置根控制器
        var root=RootViewController()
        self.window!.rootViewController=root
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

}

5.RootViewController.swift

//
//  RootViewController.swift
//  NavigationDemo
//
//  Created by 赵超 on 14-6-27.
//  Copyright (c) 2014年 赵超. All rights reserved.
//å

import UIKit

class RootViewController: UITabBarController {

    var tabBarBgImg:UIImageView?

var tabBarBgImgSelected:UIImageView?

    override func viewDidLoad() {
        super.viewDidLoad()
        //隐藏自带tabBarItem
        self.tabBar.hidden=true
        customTabBar()
        loadViewController()
    }
    //选择视图
    func test(tap:UITapGestureRecognizer){
        var view=tap.view
        var index=view.tag as Int
        var z=(index)*57
        var c=CGFloat(z)
        var x:CGFloat=5.0 + c
        var y=tabBarBgImg!.frame.size.height/2-45/2
        UIView.beginAnimations("test",context:nil)
        tabBarBgImgSelected!.frame = CGRectMake(x ,y, 50, 45)
        UIView.commitAnimations()
        //跳转页面
        self.selectedIndex=view.tag

    }

    //自己定义tabBar视图
    func customTabBar(){

        var height=UIScreen.mainScreen().bounds.size.height
        var width=UIScreen.mainScreen().bounds.size.width
        var tabW=width
        var tabH=height-49
        tabBarBgImg=UIImageView(frame:CGRectMake(0,tabH,tabW,49))
        //打开事件
        tabBarBgImg!.userInteractionEnabled=true
        tabBarBgImg!.image=UIImage(named:"tab_bg_all")

        //选中背影图片
        var y=tabBarBgImg!.frame.size.height/2-45/2
        tabBarBgImgSelected=UIImageView(frame:CGRectMake(5,y, 50, 45))
        tabBarBgImgSelected!.image=UIImage(named:"selectTabbar_bg_all1")
        tabBarBgImg!.addSubview(tabBarBgImgSelected)

        var x:CGFloat=0
        var images=["icon_cinema","msg_new"]
        var titles=["电影","新闻"]
        var titleFont=UIFont.systemFontOfSize(12)
        for  index in 0..2{
            var imgView=UIImageView(frame:CGRectMake( x+18, y+5, 22, 22))
            //加入事件
            imgView.userInteractionEnabled=true
            imgView.tag=index
            var tap=UITapGestureRecognizer(target:self,action:Selector("test:"))
            imgView.addGestureRecognizer(tap)

            imgView.image = UIImage(named:images[index])
            tabBarBgImg!.addSubview(imgView)
            var title=UILabel(frame:CGRectMake(x+16,y+26,45,15))
            title.text=titles[index]
            title.font=titleFont
            title.textColor = UIColor.whiteColor()
            tabBarBgImg!.addSubview(title)
            x+=57
        }
        self.view.addSubview(tabBarBgImg)

    }

    //载入子视图控制器
    func loadViewController(){
        //USA
        var movie=MovieViewController()
        var  movieItem=UITabBarItem(tabBarSystemItem: .Favorites,tag:1)
        movie.tabBarItem=movieItem
        var movieNav=UINavigationController(rootViewController:movie)
        //News
        var news=NewsViewController()
        var  newsItem=UITabBarItem(tabBarSystemItem: .Favorites,tag:2)
        news.tabBarItem=newsItem
        var newsNav=UINavigationController(rootViewController:news)

        //数组
        var ctrls=[movieNav,newsNav]
        //加入
        self.setViewControllers(ctrls,animated:true)
    }

}

完整代码:https://github.com/whzhaochao/CustomTabBarItem

版权声明:本文博主原创文章,博客,未经同意不得转载。

时间: 2024-08-29 12:38:56

swift 它们的定义TabBarItem的相关文章

Swift开发教程--怎样自己定义TabBarItem的图片显示

在做项目的时候,假设使用系统的UITabBarController的时候,底部的tab自己定义图片显示是蓝色和灰色的.这不是我们所想要的效果. 假设想显示自己定义的按下和弹起的图片效果.这个时候就须要对TabBarItem进行改动. 下面就是详细的演示样例: var itemNameArray:[String] = ["down_32","add_32","plane_32","circle_32"] var itemName

iOS swift 常量 && 宏定义

全局常量 在C和Objective-C语言源文件中定义的全局常量会自动地被Swift编译引进并做为Swift的全局常量. 预处理指令 Swift编译器不包含预处理器.取而代之的是,它充分利用了编译时属性,生成配置,和语言特性来完成相同的功能.因此,Swift没有引进预处理指令. 简单宏 在 C和Objective-C中,通常使用#define指令来定义一个简单的常数,在Swift,您可以使用全局常量来代替.例如:定义一个常数 的#define FADE_ANIMATION_DURATION 0.

swift -类的定义及使用

// // main.swift // ClassDemo-06 // import Foundation println("Hello, World!") //类的定义 Person 名字 class Person{ //定义一个字段 年龄 var age :Int = 0; //定义一个字段 名字 //?表示name 为空 nil 或者没有设置 name是可选择的 var name :String?; //构造方法 init() {//不带参数 age = 5; name = &q

Swift函数的定义

//: Playground - noun: a place where people can play import Cocoa //基本的函数 //*******************************************无参无返回值的函数 func first(){ print("这是我写的第一个函数") } //调用函数, //PS 小括号不可以省略,将函数赋值给其他属性才可这么做 first() //********************************

Swift 函数的定义与调用(Defining and Calling Functions)

当你定义一个函数时,你可以定义一个或多个有名字和类型的值,作为函数的输入(称为参数,parameters),也可以定义某种类型的值作为函数执行结束的输出(称为返回类型). 每个函数有个函数名,用来描述函数执行的任务.要使用一个函数时,你用函数名"调用",并传给它匹配的输入值(称作实参,arguments).一个函数的实参必须与函数参数表里参数的顺序一致. 在下面例子中的函数叫做"greetingForPerson",之所以叫这个名字是因为这个函数用一个人的名字当做输

Swift函数的定义建议

/* Swift中函数命名的智慧 */ // 1.一般情况下, 我们写一个函数是这么写的 func sayHello(name: String , greeting: String) { print("\(greeting), \(name)") } // 调用的时候是这样的, 这个函数这样读起来并不是很符合人类的阅读习惯 sayHello("Rinpe", greeting: "Hello") // 2.下面是对这个函数进行修改 func sa

swift 基本数据类型定义

infix operator & { associativity left precedence 150 } infix operator !== { associativity none precedence 130 } infix operator >= { associativity none precedence 130 } infix operator ~= { associativity none precedence 130 } infix operator < { as

swift 编译提前定义 --不知道怎么定义,可是能够#if

var v:Int; #if _COND//不知道怎么定义.可是能够#if v = 1; #else v = 2; #endif println(v);//2 原文地址:https://www.cnblogs.com/zhchoutai/p/8438134.html

swift -变量的定义与使用

使?用let来声明常量,使?用var来声明变量. ?一个常量的值在编译时并不须要获取,可是你仅仅能为它赋值?一次.也就是说你能够?用常量来表?示这样?一个值:你仅仅须要决定?一次,可是须要使?用非常多次. NSLog("--------------") var myVariable = 42 myVariable = 50 let myContant = 42 let lable = "The with is" 常量或者变量的类型必须和你赋给它们的值?一样. 然?而