1.导航条(navigationItem)属性由四部分组成:
title, leftBarButtonItem, prompt, rightBarButtonItem
//追加显示在title的上方
self.navigationItem.prompt;
2.改变导航条的颜色
self.navigationConroller.navigationBar.tintColor = [UIColor redColor];
3.导航条按钮之间的距离
<span style="color:#006600;"> //显示ToolbarHidden
[self.navigationController setToolbarHidden:NO animated:YES];
UIBarButtonItem *fontSize = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"text2"] style:UIBarButtonItemStylePlain target:self action:@selector(didFontSize)];
UIBarButtonItem *directory = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"book_28"] style:UIBarButtonItemStylePlain target:self action:@selector(didDirectory)];
UIBarButtonItem *brightness = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"sun_stroke"] style:UIBarButtonItemStylePlain target:self action:@selector(didChangeBrightness)];
//创建一个空白的按钮,调节按钮之间的距离
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
//将按钮加入数组显示在导航条上
self.toolbarItems = [NSArray arrayWithObjects:flexSpace,fontSize,flexSpace,directory,flexSpace,brightness,flexSpace, nil];</span>
对比
4、UINavigationController的结构组成
看下图,UINavigationController有Navigation bar ,Navigation View ,Navigation toobar等组成。
5.自定义导航条
LBookCityView.h
@interface LBookCityView : UIView
@property(nonatomic,strong)UIImageView *scrollBackgroundView;//背景
@property(nonatomic,strong)UIImageView *selectView;//滑动的view
+(LBookCityView *)shareInstance;
- (void)moveSelectImageView:(UIButton*)btn;
@end
LBookCityView.m
@interface LBookCityView ()
{
UIButton *_button;
NSMutableArray *_allBarButton;
}
@end
@implementation LBookCityView
+(LBookCityView *)shareInstance
{
static LBookCityView *_LBookCityView = nil;
@synchronized(self){
if (!_LBookCityView ) {
_LBookCityView = [[LBookCityView alloc]init];
}
}
return _LBookCityView;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self crectSubView];
}
return self;
}
-(void)crectSubView
{
[self changeBackGrandColor];
}
-(void)changeBackGrandColor
{
//设置背景
_scrollBackgroundView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 74)];
_scrollBackgroundView.backgroundColor = [UIColor colorWithRed:0.427 green:0.880 blue:0.892 alpha:1.000];
_scrollBackgroundView.userInteractionEnabled = YES;
[self addSubview:_scrollBackgroundView];
//设置滑块
_selectView = [[UIImageView alloc]initWithFrame:CGRectMake(42, 32, 50, 25)];
_selectView.layer.cornerRadius = 10;
_selectView.backgroundColor = [UIColor colorWithWhite:0.922 alpha:1.000];
[_scrollBackgroundView addSubview:_selectView];
//贴标签
NSArray *allButton = [NSArray arrayWithObjects:@"排行",@"分类",@"专题", nil];
_allBarButton = [NSMutableArray array];
//创建按钮
for (int i = 0; i <[allButton count]; i++) {
_button = [UIButton buttonWithType:UIButtonTypeSystem];
_button.frame = CGRectMake(100*i+44, 32, 50, 25);
[_button setTitle:[allButton objectAtIndex:i] forState:UIControlStateNormal];
_button.tag = 200+i;
//文字的颜色
[_button setTitleColor:[UIColor colorWithRed:0.953 green:0.208 blue:0.051 alpha:1.000] forState:UIControlStateHighlighted];
//圆角
_button.layer.cornerRadius = 10;
//触发的方法
switch (i) {
case 0:
{
[_button addTarget:self action:@selector(didClickSeniorityButtonItemAction:) forControlEvents:UIControlEventTouchUpInside];
}
break;
case 1:
{
[_button addTarget:self action:@selector(didClickclassifyButtonItemAction:) forControlEvents:UIControlEventTouchUpInside];
}
break;
case 2:
{
[_button addTarget:self action:@selector(didClicksubjectButtonItemAction:) forControlEvents:UIControlEventTouchUpInside];
}
break;
}
[_scrollBackgroundView addSubview:_button];
}
}
//点击排行
-(void)didClickSeniorityButtonItemAction:(UIButton *)button
{
[self moveSelectImageView:button];
[LBookCityViewController shareInstance].scrollView.contentOffset = CGPointMake(0, 0);
}
//点击分类
-(void)didClickclassifyButtonItemAction:(UIButton *)button
{
[self moveSelectImageView:button];
[LBookCityViewController shareInstance].scrollView.contentOffset = CGPointMake(320, 0);
}
//点击专题
-(void)didClicksubjectButtonItemAction:(UIButton *)button
{
[self moveSelectImageView:button];
[LBookCityViewController shareInstance].scrollView.contentOffset = CGPointMake(640, 0);
}
#pragma mark-----按钮的滑动效果-------
- (void)moveSelectImageView:(UIButton*)btn{
[UIView animateWithDuration:0.15 animations:
^(void){
CGRect frame = _selectView.frame;
frame.origin.x = btn.frame.origin.x;
_selectView.frame = frame;
} completion:nil];
}
效果图:
6.UIBarButtonSystemItemAction的风格,这是系统自带的按钮风格.
如下图: