swift-导航控制器的自定义

来源:转载

 1 //

2 // AppDelegate.swift

3 // Housekeeper

4 //

5 // Created by 卢洋 on 15/10/14.

6 // Copyright © 2015年 奈文摩尔. All rights reserved.

7 //

8

9 import Foundation

10 import UIKit

11

12 @UIApplicationMain

13 class AppDelegate: UIResponder, UIApplicationDelegate {

14

15 var window: UIWindow?

16 //var indexTab:UITabBarController?;

17

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

19 // 应用程序启动后

20 //1.声明一个空视图

21 self.window = UIWindow(frame: UIScreen.mainScreen().bounds);

22 self.window!.backgroundColor=UIColor.whiteColor();

23

24 //2.1导航背景颜色

25 UINavigationBar.appearance().barTintColor=UIColor.appMainColor();

26

27 UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(CGFloat(NSInteger.min),CGFloat(NSInteger.min)), forBarMetrics:UIBarMetrics.Default);

28 //2.2导航标题文字颜色

29 UINavigationBar.appearance().titleTextAttributes=NSDictionary(object:UIColor.whiteColor(), forKey:NSForegroundColorAttributeName) as? [String : AnyObject];

30

31 //2.3将状态栏变为白色

32 UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent;

33

34 //2.4设置返回按钮颜色

35 UINavigationBar.appearance().tintColor=UIColor.whiteColor();

36

37 //3.指定根视图

38 let rootView=NTViewController();

39 self.window!.rootViewController=rootView;

40 self.window!.makeKeyAndVisible();

41

42

43

44 //4.初始化键盘插件

45 //let manage:IQKeyboardManager=IQKeyboardManager.sharedManager();

46 //manage.enable=true;

47 //manage.shouldResignOnTouchOutside=true;

48 //manage.shouldToolbarUsesTextFieldTintColor=true;

49 //manage.enableAutoToolbar=true;

50

51 return true

52 }

53

54 func applicationWillResignActive(application: UIApplication) {

55 // 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.

56 // 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.

57 }

58

59 func applicationDidEnterBackground(application: UIApplication) {

60 // 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.

61 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

62 }

63

64 func applicationWillEnterForeground(application: UIApplication) {

65 // 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.

66 }

67

68 func applicationDidBecomeActive(application: UIApplication) {

69 // 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.

70 }

71

72 func applicationWillTerminate(application: UIApplication) {

73 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

74 }

75

76

77 }

2.下面是我自定义的导航控制器类,继承 UITabBarController

 1 //

2 // NTViewController.swift

3 // Housekeeper

4 //

5 // Created by 卢洋 on 15/10/20.

6 // Copyright © 2015年 奈文摩尔. All rights reserved.

7 //

8

9 import Foundation

10 import UIKit

11 class NTViewController:UITabBarController{

12

13 //页面初始化

14 override func viewDidLoad() {

15 super.viewDidLoad();

16 //1.创建首页导航控制器

17 let vwIndex=index();

18 let navIndex=UINavigationController(rootViewController: vwIndex);

19 navIndex.title="首页";

20 navIndex.tabBarItem.image=UIImage(named: "home.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); //默认显示图片

21 navIndex.tabBarItem.selectedImage=UIImage(named: "homes.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); //选中后的图片

22

23

24 //2.创建活动信息导航控制器

25 let vwActivityInfo=activityInfo();

26 let navActivityInfo=UINavigationController(rootViewController: vwActivityInfo);

27 navActivityInfo.title="活动信息";

28 navActivityInfo.tabBarItem.image=UIImage(named: "Activity-information.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);

29 navActivityInfo.tabBarItem.selectedImage=UIImage(named: "Activity-informations.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);

30

31

32 //3.创建车辆展示导航控制器

33 let vwCarDisplay=carDisplay();

34 let navCarDisplay=UINavigationController(rootViewController: vwCarDisplay);

35 navCarDisplay.title="车辆展示";

36 navCarDisplay.tabBarItem.image=UIImage(named: "Vehicle-display.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);

37 navCarDisplay.tabBarItem.selectedImage=UIImage(named: "Vehicle-displays.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);

38

39

40 //4.创建个人中心导航控制器

41 let vwPersonalCenter=personalCenter();

42 let navPersonalCenter=UINavigationController(rootViewController: vwPersonalCenter);

43 navPersonalCenter.title="个人中心";

44 navPersonalCenter.tabBarItem.image=UIImage(named: "Personal-Center.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);

45 navPersonalCenter.tabBarItem.selectedImage=UIImage(named: "Personal-Centers.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);

46

47 //5.添加到工具栏

48 let items=[navIndex,navActivityInfo,navCarDisplay,navPersonalCenter];

49 self.viewControllers=items;

50 self.navigationController?.navigationBar.tintColor=UIColor.whiteColor();

51

52 //6.自定义工具栏

53 self.tabBar.backgroundColor=UIColor.clearColor();

54 //底部工具栏背景颜色

55 self.tabBar.barTintColor=UIColor.appMainColor();

56

57 //7.设置底部工具栏文字颜色(默认状态和选中状态)

58 UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object:UIColor.whiteColor(), forKey:NSForegroundColorAttributeName) as? [String : AnyObject], forState:UIControlState.Normal);

59 UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object:UIColor.blueWithTabbar(), forKey:NSForegroundColorAttributeName) as? [String : AnyObject], forState:UIControlState.Selected)

60 //self.tabBar.tintColorDidChange()=UIColor.greenColor();

61 // let viewBar=UIView(frame:CGRectMake(0,0,UIScreen.mainScreen().bounds.width, 49));

62 // viewBar.backgroundColor=UIColor(patternImage:UIImage(named:"TabbarBg.png")!);

63 // self.tabBar.insertSubview(viewBar, atIndex:0)

64 // self.tabBar.opaque=true

65 // self.tabBar.tintColor=UIColor.appMainColor();

66

67 }

68

69 }

效果图如下:

 

ps:楼主的状态栏颜色改变了,设置为了白色,如果有需要,打开 info.plist  增加Key(View controller-based status bar appearance,Value为 no

 


分享给朋友:
您可能感兴趣的文章:
随机阅读: