Swift custom TabBarItem

Source: Internet
Author: User

1.



2. NewsViewController. swift

/// NewsViewController. swift // NavigationDemo /// Created by Zhao Chao on 14-6-27. // Copyright (c) 2014 Zhao Chao. all rights reserved. // import UIKitclass NewsViewController: UIViewController {override func viewDidLoad () {super. viewDidLoad () self. view. backgroundColor = UIColor. blueColor () self. title = "news "}}

3. MoviewViewController. swift
/// MovieViewController. swift // NavigationDemo /// Created by Zhao Chao on 14-6-27. // Copyright (c) 2014 Zhao Chao. all rights reserved. // import UIKitclass MovieViewController: UIViewController {override func viewDidLoad () {super. viewDidLoad () self. view. backgroundColor = UIColor. redColor () self. title = "movie "}}

4. AppDelegate. swift
//// AppDelegate. swift // NavigationDemo /// Created by Zhao Chao on 14-6-27. // Copyright (c) 2014 Zhao Chao. all rights reserved. // import UIKit @ UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {var window: UIWindow? Func application (application: UIApplication, didfinishlaunchingwitexceptions 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 () // set the root controller 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 shocould 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 changes 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 previusly 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 Zhao Chao on 14-6-27. // Copyright (c) 2014 Zhao Chao. all rights reserved. // define import UIKitclass RootViewController: UITabBarController {var tabBarBgImg: UIImageView? Var tabBarBgImgSelected: UIImageView? Override func viewDidLoad () {super. viewDidLoad () // hide built-in tabBarItem self. tabBar. hidden = true customTabBar () loadViewController ()} // select the view 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 () // jump to the page self. selectedIndex = view. tag} // custom tabBar view 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) // open the event tabBarBgImg !. UserInteractionEnabled = true tabBarBgImg !. Image = UIImage (named: "tab_bg_all") // select the back image 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 = ["movie", "news"] var titleFont = UIFont. systemFontOfSize (12) for index in 0 .. 2 {var imgView = UIImageView (frame: CGRectMake (x + 18, y + 5, 22, 22) // Add the event imgView. userInteractionEnabled = true imgView. tag = index var tap = UITapGestureRecognizer (target: self, action: Selector ("test:") imgView. addGestureRecognizer (tap) imgVie W. 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)} // load the subview controller 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 newsnv = UINavigationController (rootViewController: news) // array var ctrls = [movieNav, newsnv] // Add self. setViewControllers (ctrls, animated: true )}}


Full: https://github.com/whzhaochao/CustomTabBarItem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.