导航基本用法

导航栏图解

标题

self.navigationItem.title=@"注册账号";

导航背景色

self.navigationController.navigationBar.barTintColor=[UIColor redColor];

导航标题的颜色

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];

隐藏导航返回按钮

self.navigationItem.hidesBackButton=YES;

设置返回文字为白色

self.navigationItem.backBarButtonItem.tintColor=[UIColor whiteColor];

把导航的底部当做屏幕的原点开始计算

if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0) {
     self.navigationController.navigationBar.translucent=NO;//透明度
 }

怎么像safari一样滑动的时候隐藏navigationbar?

self.navigationController.hidesBarsOnSwipe = YES;

去掉导航黑线

[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];

self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

设置导航模拟全透明导航

[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];self.navigationController.navigationBar.barStyle=UIBarStyleBlackTranslucent;self.navigationController.navigationBar.translucent=YES;

去掉返回按钮上面的文字

[[UIBarButtonItem appearance]setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)forBarMetrics:UIBarMetricsDefault];

添加多个导航栏按钮

UIBarButtonItem
*shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action: nil nil];*
UIBarButtonItemcameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action: nil nil];
NSArray *itemsArr = @[shareItem,cameraItem];
self.navigationItem.rightBarButtonItems = itemsArr;

去掉导航下面的一条黑线

self.navigationController.navigationBar.clipsToBounds=YES;
self.automaticallyAdjustsScrollViewInsets=NO;

修改navgationBar的字体的颜色

[self.navigationController.navigationBarsetTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:[UIColorwhiteColor],NSForegroundColorAttributeName,nil]];

*** 设置背景图***

    [navBar setBackgroundImage:[UIImage imageNamed:@"1"] forBarMetrics:0];
      //或者设置背景颜色(这两种方法,都可以设置navigationBar上面的image)
    navBar.barTintColor=[UIColor yellowColor];
报错为reason: '-[UIButton _setOwningNavigationItem:]
设置自定义导航的时候,或者设置导航上面多个自定义按钮时候,放的对象都必须是UIBarButtonItem。

隐藏导航底部黑线

导航栏渐变效果

  //方法一
  - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        CGFloat offsetToShow = 200.0;//滑动多少就完全显示
        CGFloat alpha = 1 - (offsetToShow - scrollView.contentOffset.y) / offsetToShow;
        [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = alpha;
    }
//方法二
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetToShow = 200.0;
    CGFloat alpha = 1 - (offsetToShow - scrollView.contentOffset.y) / offsetToShow;
    
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor orangeColor]colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];
}

//生成一张纯色的图片
- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return theImage;
}

navigationBar变为纯透明

//第一种方法
//导航栏纯透明
//self.navigationController.navigationBar.translucent = YES; 不然该句不起作用(默认为YES)
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//去掉导航栏底部的黑线
self.navigationBar.shadowImage = [UIImage new];

//第二种方法
//self.navigationController.navigationBar.translucent = YES; 不然导航变成黑色(默认为YES)
[[self.navigationBar subviews] objectAtIndex:0].alpha = 0;

tabBar变为纯透明

[self.tabBar setBackgroundImage:[UIImage new]];
self.tabBar.shadowImage = [UIImage new];

导航左右按钮的位置调整(右按钮为例)

//所需btnItem
    UIButton *button=[UIButton new];//设置内容省略
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:button];
//间隔btnItem
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space.width = -10;//正负代表两个方向,自己试下。(左正右负)
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:space,rightItem, nil]];

导航添加多个按钮并调整位置(右侧添加两个按钮为例)

//所需btnItem
    UIButton *button1=[UIButton new];//设置内容省略
    UIButton *button2=[UIButton new];//设置内容省略
    UIBarButtonItem *btnItem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
    UIBarButtonItem *btnItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
//间隔btnItem
    UIBarButtonItem *space1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space1.width = -10;
    UIBarButtonItem *space2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space2.width = -10;
//注意数组中的顺序对应的是导航从右到左的顺序。
    self.navigationItem.rightBarButtonItems  = @[space1,btnItem1,space2,btnItem2];

自定义导航的titleview

//不能使用addSubview
//    [self.navigationItem.titleView addSubview:titlesView];
    self.navigationItem.titleView = titlesView;

//方式一:
//    self.navigationItem.titleView = self.menuScreeningView;
//方式二:(这样添加,离开该界面的时候要手动移除。)
    [self.navigationController.navigationBar addSubview:titlesView];
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //移除导航上面的下拉列表
    [titlesView removeFromSuperview];

}

设置导航的背景图拉伸

    UIImage * img= [UIImage imageNamed:@"shy_bj"];
    img = [img stretchableImageWithLeftCapWidth:1 topCapHeight:1];
    
    //导航背景
    [self.navigationController.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
设置导航的背景图拉伸

导航左边返回按钮点击范围很大的解决方案

    // 左边按钮
    UIButton *leftbut =[UIButton buttonWithType:UIButtonTypeCustom];
    
    [leftbut setImage:[UIImage imageNamed:@"btn_left"] forState:UIControlStateNormal];
    
    [leftbut addTarget:self action:@selector(ClicknavleftBut) forControlEvents:UIControlEventTouchUpInside];
    [leftbut sizeToFit];
    
    //把返回按钮放在一个view上面,然后添加到导航上,不然返回的范围很大。
    UIView *leftView = [UIView new];
    leftView.frame= leftbut.frame;
    [leftView addSubview:leftbut];
    
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:leftView];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,099评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,473评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,229评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,570评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,427评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,335评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,737评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,392评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,693评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,730评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,512评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,349评论 3 314
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,750评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,017评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,290评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,706评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,904评论 2 335

推荐阅读更多精彩内容