ios 导航栏(自定义和使用系统方式)

系统方式:

    //1.设置导航栏背景图片
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc]init];

    [[self navigationController] setNavigationBarHidden:NO animated:YES];
    self.navigationController.navigationBar.backgroundColor = [[UIColor alloc] initWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0];

    //2.导航面板左边的取消按钮
    UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    if(cancelButton != nil)
    {
        [cancelButton setTitle:POST_CANCEL_BUTTON forState: UIControlStateNormal];
        [cancelButton setFrame:CGRectMake(0, 0, WIDTH_SCREEN/5.0, 44)];
        [cancelButton setTitleColor:[[UIColor alloc] initWithRed:0 green:158/255.0 blue:150/255.0 alpha:1.0]forState:UIControlStateNormal];
        cancelButton.titleLabel.font            = [UIFont systemFontOfSize: 16.0];
        cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [cancelButton addTarget:self action:@selector(cancelButtonEventTouchUpInside)
              forControlEvents :UIControlEventTouchUpInside];

        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
        if(leftItem != nil)
        {
            self.navigationItem.leftBarButtonItem = leftItem;
        }
    }

    //3.导航面板右边的发布按钮
    UIButton* postButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    if (postButton != nil)
    {
        [postButton setFrame:CGRectMake(0, 0, WIDTH_SCREEN/5.0, 44)];
        [postButton setTitle:@"发布" forState:UIControlStateNormal];
        [postButton setTitleColor:[[UIColor alloc] initWithRed:0 green:158/255.0 blue:150/255.0 alpha:1.0]forState:UIControlStateNormal];
        postButton.titleLabel.font            = [UIFont systemFontOfSize: 16.0];
        postButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [postButton addTarget:self action:@selector(postButtonEventTouchUpInside)
            forControlEvents :UIControlEventTouchUpInside];
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:postButton];
        if(rightItem != nil)
        {
            self.navigationItem.rightBarButtonItem = rightItem;
        }
    }

    //4.导航面板中部文字
    UILabel* navigationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
    if (navigationLabel != nil)
    {
        [navigationLabel setTextColor:[UIColor blackColor]];
        navigationLabel.text = POST_NAVIGATION_TITLE;
        [navigationLabel setTextAlignment:NSTextAlignmentCenter];
        navigationLabel.font = [UIFont systemFontOfSize:18.0];
        self.navigationItem.titleView = navigationLabel;
    }

    //5.导航下面的一条分割线
    UIView* line = [[UIView alloc]initWithFrame:CGRectMake(0, 20 + 44,WIDTH_SCREEN, 1)];
    if (line != nil)
    {
        line.backgroundColor = [[UIColor alloc] initWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];
        [self.view addSubview:line];
    }

自定义:

    //1.创建导航栏视图
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN, 65)];
    if (navView != nil)//当导航视图没有加载成功的时候推出该方法
    {
        //1.为导航视图设置背景
        navView.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:248 / 255.0 blue:248 / 255.0 alpha:1];
        [[self navigationController] setNavigationBarHidden:YES animated:YES];

        //2.导航面板左边的取消按钮
        UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        if (leftButton != nil)
        {
            leftButton.frame = CGRectMake(15, 20, 65, 44);
            [leftButton setTitle:POST_CANCEL_BUTTON forState: UIControlStateNormal];
            [leftButton setTitleColor:[[UIColor alloc] initWithRed:0 green:158/255.0 blue:150/255.0 alpha:1.0]forState:UIControlStateNormal];
            leftButton.titleLabel.font            = [UIFont systemFontOfSize: 16.0];
            leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [leftButton addTarget:self action:@selector(cancelButtonEventTouchUpInside)
                forControlEvents :UIControlEventTouchUpInside];
            [navView addSubview:leftButton];
        }
        //3.导航面板右边的发布按钮
        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        if (rightButton != nil)
        {
            [rightButton setFrame:CGRectMake(WIDTH_SCREEN - 80, 20, 65, 44)];
            [rightButton setTitle:@"发布" forState:UIControlStateNormal];
            [rightButton setTitleColor:[[UIColor alloc] initWithRed:0 green:158/255.0 blue:150/255.0 alpha:1.0]forState:UIControlStateNormal];
            rightButton.titleLabel.font            = [UIFont systemFontOfSize: 16.0];
            rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            [rightButton addTarget:self action:@selector(postButtonEventTouchUpInside)
                 forControlEvents :UIControlEventTouchUpInside];
            [navView addSubview:rightButton];
        }

        //4.导航面板中部文字
        UILabel* navTitle = [[UILabel alloc] initWithFrame:CGRectMake(80, 20, WIDTH_SCREEN - 80 - 80, 44)];
        if (navTitle != nil)
        {
            [navTitle setTextColor:[UIColor blackColor]];
            navTitle.text = POST_NAVIGATION_TITLE;
            [navTitle setTextAlignment:NSTextAlignmentCenter];
            navTitle.font = [UIFont systemFontOfSize:18.0];
            [navView addSubview:navTitle];
        }

        //5.在导航视图底添加分割线
        UIView *navDividingLine = [[UIView alloc] init];
        if (navDividingLine != nil)
        {
            navDividingLine.frame = CGRectMake(0, 20 + 44, WIDTH_SCREEN, 1);
            navDividingLine.backgroundColor = [UIColor colorWithRed:221 / 255.0 green:221 / 255.0 blue:221 / 255.0 alpha:1];
            [navView addSubview:navDividingLine];
        }

        //6.往view增加导航栏
        [self.view addSubview:navView];
    }
时间: 2024-12-15 11:03:36

ios 导航栏(自定义和使用系统方式)的相关文章

iOS 为导航栏自定义按钮图案Button Image 运行出来的颜色与原本颜色不一样 -解决方案

为相机制作闪光灯,在导航栏自定义了"闪光"图案,希望点击时变换图片,但是一直没有改变,原来是因为设置了Global Tint的颜色,所以系统会自动把图片的颜色改为Global Tint的颜色. 解决方案,设置图片时,添加:imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal 源码: - (void) setFlashOn:(BOOL)isOn { if (self.captureDevice.hasFlash) { UIIm

<iOS 导航栏>第一节:导航栏透明方法实现代码

说下导航栏的透明方法: 很多应用需要导航栏随着向上滑动,逐渐从透明变成不透明,很炫酷,大部分应用都在使用导航栏渐变效果,现附上代码然后直接将实现,一会讲下如何来实现,这一部分直接上代码. 先附上代码: 方法声明: #import <UIKit/UIKit.h> @interface IDSNavBarView : UIView - (instancetype)initWithFrame:(CGRect)frame titleImg:(UIImage *)aTitleImg; - (UILabe

ios 导航栏(自己定义和使用系统方式)

系统方式: //1.设置导航栏背景图片 [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]; self.navigationController.navigationBar.shadowImage = [[UIImage alloc]init]; [[self navigationController] setNa

自定义iOS导航栏背景,标题和返回按钮文字颜色-----转载自gyz413977349

在iOS7下,默认导航栏背景,颜色是这样的,接下来我们就进行自定义,如果你仅仅是更改一下背景和颜色,代码会很简单,不需要很复杂的自定义View来替代leftBarItem 更改导航栏的背景和文字Color 方法一: [objc] view plaincopy //set NavigationBar 背景颜色&title 颜色 [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:20/255.

iOS 导航栏黑线,UIImage 枚举处理方式

ios 找出导航栏下面的黑线(可隐藏,改变样式等) http://www.jianshu.com/p/effa4a48f1e3 设置UIImage的渲染模式:UIImage.renderingMode http://blog.csdn.net/djxiaoyu_haha/article/details/40949083 着色(Tint Color)是iOS7界面中的一个.设置UIImage的渲染模式:UIImage.renderingMode重大改变,你可以设置一个UIImage在渲染时是否使用

iOS导航栏的正确隐藏方式【转】

简介:在项目中经常碰到首页顶部是无限轮播,需要靠最上面显示.有的设置导航栏为透明等一系列的方法,这个可以借助第三方.或者干脆简单粗暴的直接隐藏掉导航栏.可是push到下一个页面的时候是需要导航栏的,如何做了,这里给出两种方法. 第一种做法 -注意这里一定要用动画的方式隐藏导航栏,这样在使用滑动返回手势的时候效果最好,和上面动图一致.这样做有一个缺点就是在切换tabBar的时候有一个导航栏向上消失的动画. - (void)viewWillAppear:(BOOL)animated { [super

iOS NavigationBar 导航栏自定义

1. 设置导航栏NavigationBar的背景颜色: a)  setBarTintColor : 设置NagivationBar的颜色 也可以用 : [[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]]; (在UINavigationController执行pushViewController的界面里再次setBarTintColor后颜色还会变,说明设置的是同一个UINavigationBar,) b)  在

自定义iOS导航栏背景,标题和返回按钮文字颜色

一.导航栏的背景和文字Color: 方法一: //设置NavigationBar 背景颜色&title 颜色 [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1.0]]; [self.navigationController.navigationBar setTitleTextAttributes

iOS导航栏的正确隐藏方式

在项目中经常碰到首页顶部是无限轮播,需要靠最上面显示.有的设置导航栏为透明等一系列的方法,这个可以借助第三方.或者干脆简单粗暴的直接隐藏掉导航栏.可是push到下一个页面的时候是需要导航栏的,如何做了,这里给出两种方法. 第一种做法 -注意这里一定要用动画的方式隐藏导航栏,这样在使用滑动返回手势的时候效果最好,和上面动图一致.这样做有一个缺点就是在切换tabBar的时候有一个导航栏向上消失的动画. - (void)viewWillAppear:(BOOL)animated { [super vi