zoukankan      html  css  js  c++  java
  • swift 自定义TabBarItem

    1.效果图

     
       
     


    2.NewsViewController.swift

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

    3.MoviewViewController.swift

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


    4.AppDelegate.swift

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


    5.RootViewController.swift

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


  • 相关阅读:
    PCB打样前的注意事项
    STM32CubeMX新建工程+基本IO配置过程
    unicode gbk 转换函数
    16进制数字转换为字符
    字符转换为16进制数字
    System.IO.Directory类
    C# DataSet和DataTable详解
    DataGridView 控件详细解说
    关于Datagridview控件用法的一些总结(设置列chicun)
    获取DataGridView中的的选中行
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4863372.html
Copyright © 2011-2022 走看看