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)
} }

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

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

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

    在做项目的时候,假设使用系统的UITabBarController的时候,底部的tab自己定义图片显示是蓝色和灰色的.这不是我们所想要的效果. 假设想显示自己定义的按下和弹起的图片效果.这个时候就须要 ...

  2. 如何为Swift进行宏定义

    这阵子一直在自学Swift, 因为之前iOS的开发一直用Objective-C, 所以习惯了C语言那种宏定义方式, Swift作为一款更加安全的语言, 放弃了C语言中的宏定义, 有效的防止预编译时代码 ...

  3. Swift:宏定义

    一.简述 swift中并没有加入宏系统,C语言使用#define定义的基本常量在导入Swift时被Swift编译自动转为Swfit语言的全局变量.但复杂的宏定义不能被Swift转换.Swift中类似宏 ...

  4. swift -类的定义及使用

    // // main.swift // ClassDemo-06 // import Foundation println("Hello, World!") //类的定义 Pers ...

  5. iOS swift 常量 && 宏定义

    全局常量 在C和Objective-C语言源文件中定义的全局常量会自动地被Swift编译引进并做为Swift的全局常量. 预处理指令 Swift编译器不包含预处理器.取而代之的是,它充分利用了编译时属 ...

  6. Swift函数的定义

    //: Playground - noun: a place where people can play import Cocoa //基本的函数 //************************ ...

  7. Swift函数的定义建议

    /* Swift中函数命名的智慧 */ // 1.一般情况下, 我们写一个函数是这么写的 func sayHello(name: String , greeting: String) { print( ...

  8. 在OC中调用Swift类中定义delegate出现:Property 'delegate' not found on object of type ...

    找了许久没找到答案, 在下面的链接中, 我解决了这个问题: http://stackoverflow.com/questions/26366082/cannot-access-property-of- ...

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

    当你定义一个函数时,你能够定义一个或多个有名字和类型的值.作为函数的输入(称为參数.parameters).也能够定义某种类型的值作为函数运行结束的输出(称为返回类型). 每一个函数有个函数名,用来描 ...

随机推荐

  1. 在ubuntu安装Phabricator(转)

    前言: Phabricator是facebook团队进行codereview的一个工具,是基于php进行开发的.界面简洁优雅,是团队做代码评审的好帮手.个人认为,是当前最好的code review平台 ...

  2. Oracle自增列创建方法

    最近在做Oracle的项目,由于以前没有接触过Oracle的开发,遇到了不少的问题,比如给Oracle表添加自增列,与SQL Server就不同. Oracle没有自增字段这样的功能,但是通过触发器( ...

  3. 在配置文件(.settings、.config)中存储自定义对象

    原文:在配置文件(.settings..config)中存储自定义对象 引言 我前面曾写过一篇<使用配置文件(.settings..config)存储应用程序配置>,我在其中指出“sett ...

  4. 【LaTeX排版】LaTeX论文排版&lt;三&gt;

    A picture is worth a thousand words(一图胜千言).图在论文中的重要性不言而喻,本文主要解说图的制作与插入. 1.图像的插入     图像能够分为两大类:位图和向量图 ...

  5. 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...

  6. An Overview of Complex Event Processing

    An Overview of Complex Event Processing 复杂事件处理技术概览(一) 翻译前言:我在理解复杂事件处理(CEP)方面一直有这样的困惑--为什么这种计算模式是有效的, ...

  7. c# Use Properties Instead of Accessible Data Members

    advantage of properties: 1 properties can be used in data binding, public data member can not. 2 dat ...

  8. android 4.0 中出错 java.lang.UnsupportedOperationException

    在android4.0中  画图的时候使用: canvas.clipPath(path, Region.Op.XOR); 报错 java.lang.UnsupportedOperationExcept ...

  9. ADO.NET连接方式

    使用Command.DataReader和DataSet两种方法实现数据绑定 方法1:使用Command和DataReader SqlConnection con = new SqlConnectio ...

  10. Android 角色时间戳

    我是在用MediaRecorder进行录像时发生视频和音频不同步的问题,请教了一些人后感觉应该是没有时间戳,之前一直觉得时间戳就是给用户看的一个数据,查了一下发现不是的,以下是转载的.希望对大家实用: ...