《 Swift UITabBarController 的使用和自定义TabBar,和部分属性和代理的使用》


/*

Swift UITabBarController 的使用和自定义TabBar,和部分属性和代理的使用》

*/

/*

标签栏控制器是当前市场App的主流架构方式。它主要是用来控制视图控制器的。它所管理的控制器,都是独立的。

// Created by 周双建 on 15/12/8.

// copyright © 2015 周双建. All rights reserved.

//


import UIKit


@UIApplicationMain

class AppDelegate: UIResponder,UIApplicationDelegate,UITabBarControllerDelegate {


var window: UIWindow?



func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

// 标签栏控制器,向导航栏一样,要在 AppDelegate 里面创建

let tabbar_zsj = UITabBarController()

//创建三个视图控制器

/**********************************************************/

//一个

let VC1 = ViewController()

//单个设置属性

var image = UIImage(named:"ic_nav_dis")

//保留原图信息,否则,图片会被纯色代替

image=image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

//设置没有选中的图片

VC1.tabBarItem.image = image

//设置标题

VC1.tabBarItem.title = "成功"

//选中时的图片

var image_s = UIImage(named: "ic_nav_dis_s")

//保留原图信息,以防纯色代替

image_s = image_s?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

VC1.tabBarItem.selectedImage = image_s

/**********************************************************/


//第二个

let VC2 = TwoViewController()

// 设置选中与未选中的图片

var image_v2 = UIImage(named: "ic_nav_home")

//保色

image_v2 = image_v2?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

var image_v2_s = UIImage(named: "ic_nav_home_s")

//保色

image_v2_s = image_v2_s?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

//创建一个tabbaritem

let zsj_tababr = UITabBarItem(title: "奋斗",image: image_v2,selectedImage: image_v2_s)

VC2.tabBarItem = zsj_tababr

第三个

let VC3 = ThreeViewController()

// 使用系统的方法创建

var image_v3 = UIImage(named: "ic_nav_try_on")

// image_v3 = image_v3?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

//使用系统图标创建一个tabbaritem的对象

let zsj_tabbarV3 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Downloads,tag: 100)

zsj_tabbarV3.image = image_v3

zsj_tabbarV3.title = "勤勉"

VC3.tabBarItem = zsj_tabbarV3

/**********************************************************/

//设置标签栏的其他属性

// 第一种 代理

tabbar_zsj.delegate = self

/**********************************************************/

//设置开始选中那个控制器

tabbar_zsj.selectedindex = 1

// 标签 的设置

VC2.tabBarItem.badgeValue = "100+"

/**********************************************************/


// 将三个标签添加标签栏控制器

tabbar_zsj.viewControllers = [VC1,VC2,VC3]

//这个方法,必须在,所有的标签都设置到tabbar 上才可以设置,否则报错

tabbar_zsj.selectedViewController = VC2

/*

错误是:eason: '-[UITabBarController setSelectedViewController:] only a view controller in the tab bar controller's list of view controllers can be selected.

*/

//添加widows 上面进行显示

self.window?.rootViewController = tabbar_zsj

// Override point for customization after application launch.

return true

}

/******************************************************************/

//实现其 tabbar控制器的几种协议方法

/*

UITabBarControllerDelegate 的这个方法,用于返回一个负责管理过渡动画的 UIViewControllerAnimatedTransitioning

*/

func tabBarController(tabBarController: UITabBarController,animationControllerForTransitionFromViewController fromVC: UIViewController,toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

// 下面会给出TransitioningObject的实现,这里我们可以也可以选择用本方法fromVCtoVCtabBarController这三个参数构造出一个TransitioningObject

return nil

}

/* // 一个新东西就是 UIViewControllerAnimatedTransitioning 自己。。。有两个方法需要实现

// 这个方法负责做真正的动画,输入参数是过渡的上下文,从哪个VC过渡到哪个VC这些东西都可以从它得到。

func animateTransition(_ transitionContext: UIViewControllerContextTransitioning)

// 这个返回本过渡的持续时间

func transitionDuration(_ transitionContext:

UIViewControllerContextTransitioning) -> NSTimeInterval

/***************************************************/

//监控你选着的是哪个 控制器

func tabBarController(tabBarController: UITabBarController,didSelectViewController viewController: UIViewController) {

print(viewController)

}

/****************************************************************/

func tabBarController(tabBarController: UITabBarController,shouldSelectViewController viewController: UIViewController) -> Bool {

//标签栏是否可以选着 TRUE 可选 flase 不可

if viewController.isKindOfClass(TwoViewController){

return false

}else{

return true

}

}

/***************************************************************/

// 开始编辑,标签栏的时候调用

func tabBarController(tabBarController: UITabBarController,willBeginCustomizingViewControllers viewControllers: [UIViewController]) {

}

/*************************************************************/

func tabBarController(tabBarController: UITabBarController,willEndCustomizingViewControllers viewControllers: [UIViewController],changed: Bool) {

//将要结束标签栏的,编辑

}

/***************************************************************/

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:.

}



}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...