Swift 开发:UINavigationController和UITabBarController用法案例
2017-04-27 14:46
381 查看
//
// 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", title:
"测试项二", image:
"tabbar_discover")
tab.addChildViewController(n2);
//4
添加控制器
let nav3 =
UINavigationController();
//
底部bar名称
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, 0, 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:.
}
}
效果图:
// 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", title:
"测试项二", image:
"tabbar_discover")
tab.addChildViewController(n2);
//4
添加控制器
let nav3 =
UINavigationController();
//
底部bar名称
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, 0, 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:.
}
}
效果图:
相关文章推荐
- swift 豆瓣音乐开发案例
- swift 豆瓣音乐开发案例
- Swift基础用法(Swift开发之一)
- iOS开发之常用资讯类App的分类展示与编辑的完整案例实现(Swift版)
- 【iOS开发-51】案例学习:动画新写法、删除子视图、视图顺序、延迟方法、按钮多功能用法及icon图标和启动页设置
- Swift开发笔记之UIButton基本用法
- 【IOS开发】swift版的AFNETworking之Alamofire的用法 图片上传
- Swift 开发: 文本输入框(UITextField)的用法
- swift 豆瓣音乐开发案例
- Swift开发笔记3.Swift中setter和getter的基本用法
- Swift开发:解析xml文件案例
- ios开发swift中actionsheet的用法 从下面弹出
- 【IOS开发】swift的imageview的用法(加载图片,圆角,边框)
- Swift基础用法(Swift开发之一)
- 【iOS开发-53】案例学习:scrollView的核心用法以及无法滚动的解决办法
- Swift 开发:自定义 GroupBox 案例
- iOS开发:Swift里正则表达式的正确用法(限制TextField的文本输入,登录时判断正确的手机号)
- [Swift 开发] UICollectionView的用法
- 基于.net开发平台项目案例集锦(转)
- 基于.net开发平台项目案例集锦