zoukankan      html  css  js  c++  java
  • 关于自定义导航栏navigationbar

    本文只是把我看到的关于自定义导航栏的一些资料搜集了一下

    自定义导航栏样式的方法有两种:

    1.通过 [UINavigationBar apperance]  来给整个应用上的所有的navigationbar添加样式,这是一个全局的样式设置。

    2.当然如果你想要给当前的某一特定的navigationbar添加特定样式就可以通过self.navigationController.navigatinoBar 找到当前bar实例,再进行样式设置。

    第二种方法,想必大家都知道,相关资料一搜一大把,不累赘了。

    主要说说第一种方法,如果给整个应用的话,可以自定义一个CustomNavigationController继承自UINavigationController,然后在CustomNavController中重写init方法

    + (void)initialize {

        // 1.appearance方法返回一个导航栏的外观对象

        //修改了这个外观对象,相当于修改了整个项目中的外观

        UINavigationBar *navigationBar = [UINavigationBarappearance];

        

        [navigationBarsetBarTintColor:kColorNavBar];

        [navigationBar setTintColor:[UIColorwhiteColor]];// iOS7的情况下,设置NavigationBarItem文字的颜色

        // 3.设置导航栏文字的主题

       NSShadow *shadow = [[NSShadowalloc]init];

        [shadowsetShadowOffset:CGSizeZero];

        [navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColorwhiteColor],

                                               NSShadowAttributeName : shadow}];

    //    [navigationBar setBackgroundImage:[UIImage imageNamed:@"ic_cell_bg_selected"] forBarMetrics:UIBarMetricsDefault];

        // 4.修改所有UIBarButtonItem的外观

        UIBarButtonItem *barButtonItem = [UIBarButtonItemappearance];

        if (kIsIOS7OrMore) {

            [barButtonItemsetTintColor:[UIColorwhiteColor]];

        }else {

            // 修改item的背景图片

            //[barItem setBackgroundImage:[UIImage imageNamed:@"navigationbar_button_background.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

            //[barItem setBackgroundImage:[UIImage imageNamed:@"navigationbar_button_background_pushed.png"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

            // 修改item上面的文字样式

            NSDictionary *dict =@{NSForegroundColorAttributeName : [UIColorwhiteColor],

                                  NSShadowAttributeName : shadow};

            [barButtonItem setTitleTextAttributes:dictforState:UIControlStateNormal];

            [barButtonItem setTitleTextAttributes:dictforState:UIControlStateHighlighted];

        }

        //修改返回按钮样式

    //    [barButtonItem setBackButtonBackgroundImage:[UIImage imageNamed:@"ic_back"] forState:UIControlStateNormal barMetrics:UIBarMetricsCompact];

        // 5.设置状态栏样式

        [[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];

    }

    如果想要统一定制返回按钮样式的话,可以重写如下方法

    //重写返回按钮

    -(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

    {

        [superpushViewController:viewControlleranimated:animated];

        if (viewController.navigationItem.leftBarButtonItem ==nil && self.viewControllers.count >1) {

            viewController.navigationItem.leftBarButtonItem = [selfcreatBackButton];

        }

    }

    -(UIBarButtonItem *)creatBackButton

    {

        return [[UIBarButtonItemalloc]initWithImage:[UIImageimageNamed:@"ic_back"]style:UIBarButtonItemStylePlaintarget:selfaction:@selector(popSelf)];

    //或[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(popSelf)];

    }

    -(void)popSelf

    {

        [selfpopViewControllerAnimated:YES];

    }

    另外,说点导航栏的小细节:

    ios7  之后,默认样式的状态栏和导航栏是连在一起的,但是实际上导航栏的位置和大小是和之前系统版本一样的,依然是贴在状态栏下面,依然是高44px;之所以用户看来它们是连在一起,这是因为UINavigationBar里面的_UINavigationBarBackground定位在y方向-20px的位置,然后高度增加到64px,这样就可以同时充当了两者的背景。所以你需要提供高度为64px的导航条背景图。

    关于自定义导航栏的那些事儿 UINavigationController

  • 相关阅读:
    uva 1637 Double Patience
    Emacs ^ Vim
    uva 11181 Probability|Given
    uva 10491 Cows and Cars
    uva 1636 Headshot
    hdu 4336 Card Collector
    zoj 3640 Help Me Escape
    Codeforces 148 D Bag of mice
    hdu 4405 Aeroplane chess
    hdu 3853 LOOPS
  • 原文地址:https://www.cnblogs.com/dexjay/p/4812341.html
Copyright © 2011-2022 走看看