码迷,mamicode.com
首页 > 移动开发 > 详细

ios 导航控制器

时间:2015-08-19 22:48:51      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

1.在AppDelegate.m中

引用

#import "RootViewController.h"

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

#pragma mark - 配置window

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    

#pragma mark - 配置根控制器

    RootViewController *rootVC = [[RootViewController alloc] init];

    

    // 配置导航控制器

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootVC];

    

    // 配置导航栏

    

    // 1. 设置标题

    rootVC.title = @"导航栏";

    // 2. 设置颜色

    // 2.1 设置主色调

    nav.navigationBar.tintColor = [UIColor purpleColor];

    // 2.2 设置bar色调

    nav.navigationBar.barTintColor = [UIColor redColor];

    // 3. 设置背景图片

    [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg_ios7"] forBarMetrics:UIBarMetricsDefault];

    

    // 4. 设置标题文本属性

    // 4.1 设置阴影

    NSShadow *shadow = [[NSShadow alloc] init];

    shadow.shadowOffset = CGSizeMake(10, 10);

    // 4.2 设置字典

    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:25], NSForegroundColorAttributeName: [UIColor blueColor], NSShadowAttributeName: shadow};

    // 4.3 进行关联

    nav.navigationBar.titleTextAttributes = attributes;

    self.window.rootViewController = nav;

    return YES;

}

 

2.在RootViewController.m中

 

//

//  ViewController.m

//  UI_Lesson4-导航控制器

//

//  Created by archerzz on 15/8/17.

//  Copyright (c) 2015 archerzz. All rights reserved.

//

 

#import "RootViewController.h"

 

#define BUTTON_ITEM_TAG 100

 

typedef NS_ENUM(NSInteger, ButtonItem) {

    ZHButtonItemRefresh,

    ZHButtonItemShare,

    ZHButtonItemBackToRoot,

    ZHButtonItemNext

};

@interface RootViewController ()

 

- (void)initUserInterface;

 

@end

 

@implementation RootViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [self initUserInterface];

}

 

- (void)initUserInterface {

    

    self.title = @"推送导航";

    

    

    // 配置随机背景

    CGFloat red = arc4random()  % 255 / 255.0;

    CGFloat blue = arc4random() % 255 / 255.0;

    CGFloat green = arc4random() % 255 / 255.0;

    

    

    self.view.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];

    

    

    

    // 1. 创建一个导航按钮

    UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(handleBarEvent:)];

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(handleBarEvent:)];

    // 设置tag

    refreshItem.tag = BUTTON_ITEM_TAG + ZHButtonItemRefresh;

    item.tag = BUTTON_ITEM_TAG + ZHButtonItemShare;

    

    // 2. 进行关联

    self.navigationItem.rightBarButtonItems = @[refreshItem, item];

    

#pragma mark - 工具栏

    // 1. 关闭隐藏

    self.navigationController.toolbarHidden = NO;

    // 2. 创建按钮

    UIBarButtonItem *backToRootItem = [[UIBarButtonItem alloc] initWithTitle:@"backToRoot" style:UIBarButtonItemStylePlain target:self action:@selector(handleBarEvent:)];

    UIBarButtonItem *nextItem = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:@selector(handleBarEvent:)];

    

    // 2.1 创建弹性按钮

    UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    // 3. 关联按钮

    self.toolbarItems = @[flexibleItem, backToRootItem, flexibleItem, nextItem, flexibleItem];

    

    backToRootItem.tag = BUTTON_ITEM_TAG + ZHButtonItemBackToRoot;

    nextItem.tag = BUTTON_ITEM_TAG + ZHButtonItemNext;

    

#pragma mark - 配置返回按钮

    

    self.navigationController.navigationBar.backIndicatorImage = [UIImage imageNamed:@"back_btn"];

    self.navigationController.navigationBar.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"back_btn"];

     UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];

      self.navigationItem.backBarButtonItem = backItem;

 

}

 

- (void)handleBarEvent:(UIBarButtonItem *)sender {

    NSLog(@"%s", __func__);

    if (sender.tag == 100) {

        NSLog(@"刷新");

    } else {

        NSLog(@"分享");

    }

    

    ZHButtonItem index = sender.tag - BUTTON_ITEM_TAG;

    switch (index) {

        case ZHButtonItemRefresh:

            NSLog(@"刷新");

            break;

        case ZHButtonItemShare:

            NSLog(@"分享");

            break;

        case ZHButtonItemNext:

            [self next];

            break;

        case ZHButtonItemBackToRoot:

            [self backToRoot];

            break;

        default:

            break;

    }

}

 

- (void)next {

    

    // 1. 创建对象

    RootViewController *rootVC = [[RootViewController alloc] init];

    NSLog(@"self = %@, roovc = %@", self, rootVC);

    

    // 2. 导航推送

    [self.navigationController pushViewController:rootVC animated:YES];

    

}

 

- (void)backToRoot {

    [self.navigationController popToRootViewControllerAnimated:YES];

    

}

 

@end

 

 

ios 导航控制器

标签:

原文地址:http://www.cnblogs.com/HwangKop/p/4743296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!