导航控制器(二 自定义UINaigationControl)


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>


有flexSpace和没有的对比

          对比      

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的风格,这是系统自带的按钮风格.

如下图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值