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. 最牛B的编程套路

    最近,我大量阅读了Steve Yegge的文章.其中有一篇叫“Practicing Programming”(练习编程),写成于2005年,读后令我惊讶不已: 与你所相信的恰恰相反,单纯地每天埋头于工 ...

  2. Java流读写

    写: package com.wjy.write; import java.io.BufferedWriter; import java.io.FileOutputStream; import jav ...

  3. 基于HttpClient 4.3的可訪问自签名HTTPS网站的新版工具类

    本文出处:http://blog.csdn.net/chaijunkun/article/details/40145685,转载请注明.因为本人不定期会整理相关博文,会对相应内容作出完好.因此强烈建议 ...

  4. SQL参数化查询的问题

    最近碰到个问题, SQL语句中的 "... like '%@strKeyword%'"这样写查不出结果, 非的写成 "... like '%" + strKey ...

  5. OCP读书笔记(17) - 计划任务

    轻量级作业: 也称为持久性轻量级作业,如果当我们的数据库每秒钟需要创建.删除或修改数十个或数百个作业时,使用轻量级作业是降低开销的最佳方法 常规作业:是由oracle 11g Scheduler 所支 ...

  6. python学习笔记之十:文件和素材

    这里将介绍函数和对象--文件和流,让你在程序调用期间存储数据,并且可以处理来自其他程序的数据. 一. 打开文件 1.1 open函数 open函数用来打开文件,语法如下:open(name,[.mod ...

  7. Java4Android之BlockingQueue

    在研究Smack的源码的时候,我对它的连接Connection以及派生类XMPPConnection的关注是最多的,由于一个即时通信程序,它的网络模块必是它的核心. 而我非常在乎它是怎样实现的. 在收 ...

  8. contextmenu

    void Loaded(object sender, RoutedEventArgs e) { ContextMenu contextMenu = new ContextMenu(); context ...

  9. 实现strcmp非常easy的思维

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> void strcom(char *s ...

  10. Objective C (iOS) for Qt C++ Developers(iOS开发,Qt开发人员需要了解什么?)

    Qt/C++开发人员眼中的Obj-C      对于我们第一次自己定义iOS应用来说,对于来自Qt/C++开发人员来说,我不得不学习Objective-C相关语法与知识 为了让读者可以更easy理解这 ...