年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2723|回复: 0

iOS6创建iOS7风格导航导,导航条自动添加返回按钮

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-8-9 23:29:29 | 显示全部楼层 |阅读模式
    - (void)viewDidLoad
    {
        [super viewDidLoad];

        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) {

            // 1、修改导航条样式
            UINavigationBar *bar = [UINavigationBar appearance];
            // 修改导航条背景
            [bar setBackgroundImage:[UIImage imageNamed:@"navigationbar_background.png"] forBarMetrics:UIBarMetricsDefault];
            // 修改导航条文字样式
            [bar setTitleTextAttributes:@{
                                          UITextAttributeTextColor : [UIColor blackColor],
                                          UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetZero]
                                          }];


            // 2、修改导航条按钮样式
            UIBarButtonItem *item = [UIBarButtonItem appearance];

            UIImage *normal = [UIImage imageNamed:@"navigationbar_button_background.png"];
            UIImage *highlight = [UIImage imageNamed:@"navigationbar_button_background_pushed.png"];

            // 修改导航条按钮背景
            [item setBackgroundImage:normal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
            [item setBackgroundImage:highlight forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

            // 修改导航条按钮文字样式
            [item setTitleTextAttributes:@{
                                           UITextAttributeTextColor : kBlueColor,
                                           UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetZero],
                                           } forState:UIControlStateNormal];
            [item setTitleTextAttributes:@{
                                           UITextAttributeTextColor : kBlueColor,
                                           UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetZero]
                                           } forState:UIControlStateHighlighted];

            [item setBackButtonBackgroundImage:normal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
            [item setBackButtonBackgroundImage:highlight forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

            // 3、修改状态栏样式
            [UIApplication sharedApplication].statusBarHidden = NO;
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;

        }
    }

    #pragma mark - 2、代理方法
    #pragma mark 2.1、控制器将要视图显示
    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
        // 在未自定义导航条返回键的情况下,iOS6及以下版本设备导航条自动添加仿iOS7返回按钮(根控制器例外)
        if (!(viewController == navigationController.viewControllers.firstObject || [(RMBaseVCtrl *)viewController isCustomNBI]) && [viewController isKindOfClass:[RMBaseVCtrl class]]){

            // 栈控制器数组
            NSArray *vcs = navigationController.viewControllers;

            // 前一个控制器序号
            int penultimate = (int)vcs.count - 2;

            if (penultimate >= 0) {

                // 前一个控制器标题
                NSString *title = [[vcs objectAtIndex:penultimate] title];

                if (title == nil || title.length > 4) title = @"返回";

                // 返回按钮文字为前一个控制器标题、基本功能为返回上一级目录
                viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithBackButtonTitle:title addTarget:self action:@selector(barButtonItemBack:)];
            }
        }
    }

    #pragma mark 2.2、控制器完成视图显示
    - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
    //    NSLog(@"def");
    }

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-1 14:33 , Processed in 0.049375 second(s), 23 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表