Swift 开发:UINavigationController和UITabBarController用法案例

Swift 开发:UINavigationController和UITabBarController用法案例,第1张

概述// //  AppDelegate.swift //  test // //  Created by 开发 on 17/4/24. //  Copyright (c) 2017年 黄涛. All rights reserved. // import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDele

//

// 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();

// 底部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,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 开发:UINavigationController和UITabBarController用法案例全部内容,希望文章能够帮你解决Swift 开发:UINavigationController和UITabBarController用法案例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1063587.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存