//
// AppDelegate.swift
// test
// Created by 开发 on 17/4/24.
// Copyright (c) 2017年 黄涛. All rights reserved.
//
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder,UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds);
window?.backgroundColor = UIColor.whiteColor();
//更改顶部bar背景颜色
let navBar = UINavigationBar.appearance();
// 蓝色
let blue = UIColor(red: 2/255,green: 130/255,blue: 255/255,alpha: 1.0);
let black = UIColor(red: 66/255,green: 66/255,blue: 66/255,alpha: 1.0);
//设置NavigationBar背景颜色,顶部背景颜色->蓝色
navBar.barTintColor = black
// 设置顶部字体颜色-》白色,字体大小 26,字体bodoni ornaments
let dic:[String:AnyObject]? = [NSForegroundColorAttributeName:UIColor.whiteColor(),NSFontAttributeName :UIFont(name: "Bodoni Ornaments",size: 20)!];
navBar.titleTextAttributes = dic;
let barItem = UITabBar.appearance();
//底部bar 字体颜色
barItem.tintColor = blue
//1 创建根控制器,在根控制器中添加三个字控制器
let tab = UITabBarController();
tab.title = "tabtabtab";
//tab.tabBar.backgroundColor = UIColor.redColor();
//2 根据类名Test01NC添加控制器
let n1 = createController("Test01NC",title: "测试项一",image: "tabbar_discover")
tab.addChildViewController(n1);
//3 根据类名Test02NC添加控制器
let n2 = createController("Test02NC",27)">测试项二",129)"> tab.addChildViewController(n2);
//4 添加控制器
let nav3 = UINavigationController();
nav3.title = "测试项三";
//默认图片
nav3.tabBarItem.image = UIImage(named: "tabbar_discover")
//选中后的图片
nav3.tabBarItem.selectedImage = UIImage(named: "tabbar_discover_highlighted")
//创建view控制器
let vc3 = UIViewController();
//设置view为灰色
vc3.view.backgroundColor = UIColor.grayColor();
// 顶部名称
vc3.view.tintColor = UIColor.redColor();
vc3.title = "测试项三"
//将view控制器添加到导航控制器中
nav3.addChildViewController(vc3)
//将nav3添加到UITabBarController中
tab.addChildViewController(nav3);
// 设置根控制器
window?.rootViewController = tab;
// 显示
window?.makeKeyAndVisible();
return true
}
/**
根据字符串创建 UINavigationController
- parameter childControllerName: 控制器名称
- parameter title: 控制器标题
- parameter image: 控制器图片
- returns: UINavigationController
*/
func createController(childControllerName: String,title: String,image: String) ->UINavigationController {
// 动态获取命名空间
let ns = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
// 0 字符串转类
let cls: AnyClass? = NSClassFromString(ns + "." + childControllerName)
// 通过类创建对象, 不能用cls.init(),有的类可能没有init方法
// 需将cls转换为制定类型,也就是
let vcCls = cls as! UIViewController.Type
// 创建对象
let childController:UIViewController = vcCls.init()
// 1.1 设置首页tabbar对应的数据
childController.tabBarItem.image = UIImage(named: image)
//childController.tabBarItem.imageInsets = UIEdgeInsetsMake(0,80,80);
childController.tabBarItem.selectedImage = UIImage(named: image + "_highlighted")
childController.title = title
//home.tabBarItem.title = "首页"
//home.navigationItem.title = "首页"
// 2 给首页包装一个导航控制器
let nav = UINavigationController()
nav.addChildViewController(childController)
return nav;
}
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/321782.html