1.效果图

   

2.NewsViewController.swift

[objc] view plaincopy
  1. //
  2. //  NewsViewController.swift
  3. //  NavigationDemo
  4. //
  5. //  Created by 赵超 on 14-6-27.
  6. //  Copyright (c) 2014年 赵超. All rights reserved.
  7. //
  8. import UIKit
  9. class NewsViewController: UIViewController {
  10. override func viewDidLoad() {
  11. super.viewDidLoad()
  12. self.view.backgroundColor=UIColor.blueColor()
  13. self.title="新闻"
  14. }
  15. }

3.MoviewViewController.swift

[objc] view plaincopy
  1. //
  2. //  MovieViewController.swift
  3. //  NavigationDemo
  4. //
  5. //  Created by 赵超 on 14-6-27.
  6. //  Copyright (c) 2014年 赵超. All rights reserved.
  7. //
  8. import UIKit
  9. class MovieViewController: UIViewController {
  10. override func viewDidLoad() {
  11. super.viewDidLoad()
  12. self.view.backgroundColor=UIColor.redColor()
  13. self.title="电影"
  14. }
  15. }

4.AppDelegate.swift

[objc] view plaincopy
  1. //
  2. //  AppDelegate.swift
  3. //  NavigationDemo
  4. //
  5. //  Created by 赵超 on 14-6-27.
  6. //  Copyright (c) 2014年 赵超. All rights reserved.
  7. //
  8. import UIKit
  9. @UIApplicationMain
  10. class AppDelegate: UIResponder, UIApplicationDelegate {
  11. var window: UIWindow?
  12. func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
  13. self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
  14. // Override point for customization after application launch.
  15. self.window!.backgroundColor = UIColor.whiteColor()
  16. self.window!.makeKeyAndVisible()
  17. //设置根控制器
  18. var root=RootViewController()
  19. self.window!.rootViewController=root
  20. return true
  21. }
  22. func applicationWillResignActive(application: UIApplication) {
  23. // 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.
  24. // 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.
  25. }
  26. func applicationDidEnterBackground(application: UIApplication) {
  27. // 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.
  28. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  29. }
  30. func applicationWillEnterForeground(application: UIApplication) {
  31. // 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.
  32. }
  33. func applicationDidBecomeActive(application: UIApplication) {
  34. // 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.
  35. }
  36. func applicationWillTerminate(application: UIApplication) {
  37. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  38. }
  39. }

5.RootViewController.swift

[objc] view plaincopy
  1. //
  2. //  RootViewController.swift
  3. //  NavigationDemo
  4. //
  5. //  Created by 赵超 on 14-6-27.
  6. //  Copyright (c) 2014年 赵超. All rights reserved.
  7. //å
  8. import UIKit
  9. class RootViewController: UITabBarController {
  10. var tabBarBgImg:UIImageView?
  11. var tabBarBgImgSelected:UIImageView?
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. //隐藏自带tabBarItem
  15. self.tabBar.hidden=true
  16. customTabBar()
  17. loadViewController()
  18. }
  19. //选择视图
  20. func test(tap:UITapGestureRecognizer){
  21. var view=tap.view
  22. var index=view.tag as Int
  23. var z=(index)*57
  24. var c=CGFloat(z)
  25. var x:CGFloat=5.0 + c
  26. var y=tabBarBgImg!.frame.size.height/2-45/2
  27. UIView.beginAnimations("test",context:nil)
  28. tabBarBgImgSelected!.frame = CGRectMake(x ,y, 50, 45)
  29. UIView.commitAnimations()
  30. //跳转页面
  31. self.selectedIndex=view.tag
  32. }
  33. //自定义tabBar视图
  34. func customTabBar(){
  35. var height=UIScreen.mainScreen().bounds.size.height
  36. var width=UIScreen.mainScreen().bounds.size.width
  37. var tabW=width
  38. var tabH=height-49
  39. tabBarBgImg=UIImageView(frame:CGRectMake(0,tabH,tabW,49))
  40. //打开事件
  41. tabBarBgImg!.userInteractionEnabled=true
  42. tabBarBgImg!.image=UIImage(named:"tab_bg_all")
  43. //选中背影图片
  44. var y=tabBarBgImg!.frame.size.height/2-45/2
  45. tabBarBgImgSelected=UIImageView(frame:CGRectMake(5,y, 50, 45))
  46. tabBarBgImgSelected!.image=UIImage(named:"selectTabbar_bg_all1")
  47. tabBarBgImg!.addSubview(tabBarBgImgSelected)
  48. var x:CGFloat=0
  49. var images=["icon_cinema","msg_new"]
  50. var titles=["电影","新闻"]
  51. var titleFont=UIFont.systemFontOfSize(12)
  52. for  index in 0..2{
  53. var imgView=UIImageView(frame:CGRectMake( x+18, y+5, 22, 22))
  54. //添加事件
  55. imgView.userInteractionEnabled=true
  56. imgView.tag=index
  57. var tap=UITapGestureRecognizer(target:self,action:Selector("test:"))
  58. imgView.addGestureRecognizer(tap)
  59. imgView.image = UIImage(named:images[index])
  60. tabBarBgImg!.addSubview(imgView)
  61. var title=UILabel(frame:CGRectMake(x+16,y+26,45,15))
  62. title.text=titles[index]
  63. title.font=titleFont
  64. title.textColor = UIColor.whiteColor()
  65. tabBarBgImg!.addSubview(title)
  66. x+=57
  67. }
  68. self.view.addSubview(tabBarBgImg)
  69. }
  70. //加载子视图控制器
  71. func loadViewController(){
  72. //USA
  73. var movie=MovieViewController()
  74. var  movieItem=UITabBarItem(tabBarSystemItem: .Favorites,tag:1)
  75. movie.tabBarItem=movieItem
  76. var movieNav=UINavigationController(rootViewController:movie)
  77. //News
  78. var news=NewsViewController()
  79. var  newsItem=UITabBarItem(tabBarSystemItem: .Favorites,tag:2)
  80. news.tabBarItem=newsItem
  81. var newsNav=UINavigationController(rootViewController:news)
  82. //数组
  83. var ctrls=[movieNav,newsNav]
  84. //添加
  85. self.setViewControllers(ctrls,animated:true)
  86. }
  87. }
完整代码:https://github.com/whzhaochao/CustomTabBarItem

转载于:https://www.cnblogs.com/Free-Thinker/p/4863372.html

swift 自定义TabBarItem相关推荐

  1. swift:自定义UICollectionViewFlowLayout

    swift:自定义UICollectionViewFlowLayout 写作目的 UICollectionView是ios中一个十分强大的控件,利用它能够十分简单的实现一些很好看的效果.UIColle ...

  2. swift 自定义滑动视图_在Swift中创建一个向上滑动菜单视图(以编程方式)

    swift 自定义滑动视图 This is a quick tutorial on how to create a slide-up menu view in iOS 这是有关如何在iOS中创建向上滑 ...

  3. Swift - 自定义UIActivity分享

    UIActivity可以十分方便地将文字.图片等内容进行分享,比如分享到微信.微博.发送邮件.短信等等.我们不仅可以分享内容出来,也可以在自己的App里添加自己的分享按钮或隐藏已有的分享按钮来实现定制 ...

  4. Swift - 自定义单元格实现微信聊天界面

    1,下面是一个放微信聊天界面的消息展示列表,实现的功能有: (1)消息可以是文本消息也可以是图片消息 (2)消息背景为气泡状图片,同时消息气泡可根据内容自适应大小 (3)每条消息旁边有头像,在左边表示 ...

  5. Swift——自定义转场动画(一)

    弹窗转场/过度动画(Popover效果) 避免浪费大家时间,快速查看运行效果可以直接拉到最后看 [五.完整代码] 部分,如果要看递推逻辑,可以从前往后看. 一.基本设置 弹出一个控制器:系统提供了以下 ...

  6. swift 自定义画渐变色折线图

    Github上的demo下载地址:https://github.com/JayFwj/swift---/tree/master/publicVersion ====================== ...

  7. 创建 Swift 自定义集合类

    原文:Building a Custom Collection in Swift 作者:Eric Cerney 译者:kmyhy 数组.字典和集合是常见的集合类型,它们都内置在 Swift 标准库中. ...

  8. Swift自定义导航栏返回按钮

    如何去除swift系统自带的导航栏返回按钮?可以自定义返回按钮 在swift中,怎么替换系统自带的导航栏返回按钮?比如说我要替换成一张返回按钮图片,点击返回到上一页 首先,看一下系统自带的导航栏返回按 ...

  9. Swift 自定义UITableView

    github demo:https://github.com/LINGLemon/LXFSwiftApp UITableView是我们开发过程中比较常用的,用于显示一系列对象,UITableView继 ...

最新文章

  1. 关于Dreamweaver乱码问题的解决方案
  2. 第十五届全国大学生智能车东北赛区成绩
  3. windows下快速启动或关闭系统服务方法
  4. 使用Api分析器与Windows兼容包来编写智能的跨平台.NET Core应用
  5. 基于android平台的24点游戏设计与实现需求分析,基于Android平台的24点游戏设计与实现需求分析_毕业设计论文.doc...
  6. [css] 如何隐藏没有静音、自动播放的音视频?
  7. python 定义变量_python-003-变量
  8. selinux= 为 disabled_Selinux安全加固
  9. java 基本数据类型及自动类型转换
  10. php怎样空格分开输入三个数,php函数在每一空行拆分一个数组?
  11. HTML的表格边框的合并
  12. Unity 关于Activator.CreateInstance使用
  13. php的样式怎么设置字体大小,css中如何改变字体大小
  14. H5 捕鱼游戏搭建教程
  15. JavaScript基础第02天—运算符(操作符)—流程控制—循环—代码规范
  16. 基于SSM实现的物流管理系统【附源码】(毕设)
  17. 传智黑马Java → 28同步技术的原理(同步代码块)
  18. ORB-SLAM2源码笔记(1)——框架结构
  19. 递归3: 汉诺塔的递归与迭代实现
  20. 【Boost C++ 库】共享内存详解

热门文章

  1. pytorch nn.MSELoss
  2. python __str__
  3. 2.2 logistic 回归
  4. matplotlib 颜色板
  5. scipy.stats.norm
  6. 计算机网络讨论课感悟,计算机网络课程学习心得体会
  7. python爬取网页内容_Python爬虫原理解析
  8. html按照字数分页,纯javascript实现分页(两种方法)
  9. 专有网络 VPC > 快速入门 > 网络规划
  10. 金融云 > 产品常见问题 > 金融云VPC端口限制