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

iOS开发导航栏控件的作用

时间:2015-08-12 23:30:06      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

一,在iOS开发过程中针对一些导航栏上需要自定义视图的情况,有时候需要用系统自带的处理,有些时候需要自定义一些视图并把视图添加上去,这时候主要是它们的位置有些许差别,下面简单写下demo;

1,用导航栏系统自带的视图处理:

 1     //1 中间的图片
 2     UIImageView *imageBarView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth / 2.f - 40.f, 20.f, 80, 30)];
 3     imageBarView.image = [UIImage imageNamed:@"中间视图名称"];
 4     self.navigationItem.titleView = imageBarView;
 5     
 6     
 7     //2 导航栏右边的分类按钮
 8     UIButton *buttonRightItem = [UIButton buttonWithType:UIButtonTypeCustom];
 9     buttonRightItem.frame = CGRectMake(0.f, 0.f, 50.f, 30.f);
10     [buttonRightItem setTitle:@"分类" forState:UIControlStateNormal];
11     [buttonRightItem setTitleColor:[UIColor colorWithRed:0.310 green:0.598 blue:0.156 alpha:1.000] forState:UIControlStateNormal];
12     buttonRightItem.titleLabel.font = [UIFont systemFontOfSize:17];
13     [buttonRightItem addTarget:self action:@selector(buttonRightAction:) forControlEvents:UIControlEventTouchUpInside];
14     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonRightItem];
15     
16     //3 导航栏左侧的APP按钮
17     UIButton *buttonLeftItem = [UIButton buttonWithType:UIButtonTypeCustom];
18     buttonLeftItem.frame = CGRectMake(0.f, 0.f, 64.f, 64.f);
19     [buttonLeftItem setImage:[UIImage imageNamed:@"左侧视图"] forState:UIControlStateNormal];
20     buttonLeftItem.imageEdgeInsets = UIEdgeInsetsMake(0.f, -30.f, 0.f, 0.f);//用来调节相对位置
21     [buttonLeftItem addTarget:self action:@selector(buttonLeftAction:) forControlEvents:UIControlEventTouchUpInside];
22     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonLeftItem];

其实以上关键是:

self.navigationItem.titleView = imageBarView;

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonRightItem];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonLeftItem];

主要是上面的三句话,这个优点是左,中,右三个按钮的响应事件的区域比较大,比较容易调控,一般建议用系统自带的来操作;

缺点也有,就是每个区域只能放一个控件,下面用自定义的方法来在同一个区域放两个或以上的控件;

2,自定义导航栏上的视图;

 1      rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
 2     rightButton.backgroundColor = [UIColor blackColor];
 3     [rightButton setTitle:@"分享" forState:UIControlStateNormal];
 4     [rightButton addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
 5     rightButton.frame = CGRectMake(self.view.width - 50.f,0.f, 50.f, 44.f);
 6     [self.navigationController.navigationBar addSubview:rightButton];
 7     
 8     rightTwoButton = [UIButton buttonWithType:UIButtonTypeCustom];
 9     rightTwoButton.backgroundColor = [UIColor blueColor];
10     [rightTwoButton setTitle:@"收藏" forState:UIControlStateNormal];
11     [rightTwoButton addTarget:self action:@selector(storeAction:) forControlEvents:UIControlEventTouchUpInside];
12     rightTwoButton.frame = CGRectMake(self.view.width - 100.f, 0.f, 50.f, 44.f);
13     [self.navigationController.navigationBar addSubview:rightTwoButton];

 

以上方法可以把视图很任性的放在导航栏的任何位置,因为用的是 self.navigationController.navigationBar 来添加的,这个是代表整个导航栏,但是这个的frame需要认真调节,不然的话布局会很难看,除此之外,当当起视图要出现或者消失时还需要对这个按钮进行处理;

 1 - (void)viewWillDisappear:(BOOL)animated
 2 {
 3     [super viewWillDisappear:animated];
 4     
 5     [UIView animateWithDuration:0.2 animations:^{
 6         
 7         rightButton.alpha = 0;
 8         rightTwoButton.alpha = 0;
 9 
10     }];
11 }
12 
13 - (void)viewWillAppear:(BOOL)animated{
14     
15     [super viewWillAppear:animated];
16     [UIView animateWithDuration:0.2 animations:^{
17         
18         rightButton.alpha = 1;
19         rightTwoButton.alpha = 1;
20         
21     }];
22 
23 }

以上就是导航栏上视图的处理,可能比较简单,但是对初学者来说也是比较重要的;

小技巧:消除导航栏下方的阴影办法:

// 添加上这一句,可以去掉导航条下边的shadowImage,就可以正常显示了  
 self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];  
 self.navigationController.navigationBar.translucent = NO; 

 

 

 

iOS开发导航栏控件的作用

标签:

原文地址:http://www.cnblogs.com/RiversMa/p/4725735.html

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