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

前端之家收集整理的这篇文章主要介绍了《 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:.

}



}

原文链接:https://www.f2er.com/swift/325184.html

猜你在找的Swift相关文章