UI控件笔记(十四):UI之自定义导航条的封装

使用自定义导航条注意:

隐藏系统导航条

self.navigationController.navigationBarHidden =YES;


一、导航条左右按钮是图片形式——图片上无文字


1.Nav.h文件

#import <UIKit/UIKit.h>


@interface Nav : UIView


-(id)initWithFrame:(CGRect)frame andLeftBtnTitle:(NSString*)leftTitle andLeftBtnAction:(SEL)leftAction andRightBtnTitle:(NSString*)rightTitle andRightBtnAction:(SEL)rightAction andTarget:(id)target andTitle:(NSString*)title;


@end


2.Nav.m文件

#import "Nav.h"


@implementation Nav


-(id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if(self)

    {

       

    }

    

    returnself;

}


-(id)initWithFrame:(CGRect)frame andLeftBtnTitle:(NSString *)leftTitle andLeftBtnAction:(SEL)leftAction andRightBtnTitle:(NSString *)rightTitle andRightBtnAction:(SEL)rightAction andTarget:(id)target andTitle:(NSString *)title

{

    self = [super initWithFrame:frame];

    if(self)

    {

        //背景图片

        UIImageView *backImage = [[UIImageView alloc] initWithFrame:self.bounds];

        backImage.image = [UIImage imageNamed:@"navigationbar.png"];

        [self addSubview:backImage];

        [backImage release];

        //居中label

        UILabel *titleLabel = [[UILabel alloc] initWithFrame:self.bounds];

        titleLabel.backgroundColor = [UIColor clearColor];

        titleLabel.text = title;

        titleLabel.textAlignment = NSTextAlignmentCenter;

        titleLabel.textColor = [UIColor colorWithRed:103/255.0 green:168/255.0 blue:232/255.0 alpha:1];

        titleLabel.font = [UIFont boldSystemFontOfSize:22];

        [self addSubview:titleLabel];

        [titleLabel release];

        //btn

        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        leftBtn.frame = CGRectMake(20,7, 44,30);

        [leftBtn setImage:[UIImage imageNamed:@"buttonbar_action.png"] forState:UIControlStateNormal];

        [leftBtn addTarget:target action:leftAction forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:leftBtn];

        

        UILabel *leftBtnLabel = [[UILabel alloc] initWithFrame:leftBtn.bounds];

        leftBtnLabel.textAlignment = NSTextAlignmentCenter;

        leftBtnLabel.text = leftTitle;

        [leftBtn addSubview:leftBtnLabel];

        [leftBtnLabel release];

        leftBtnLabel.userInteractionEnabled = NO;

        

        //btn

        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        rightBtn.frame = CGRectMake(self.frame.size.width-64,7, 44,30);

        [rightBtn setImage:[UIImage imageNamed:@"buttonbar_action.png"] forState:UIControlStateNormal];

        [rightBtn addTarget:target action:rightAction forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:rightBtn];

        

        UILabel *rightBtnLabel = [[UILabel alloc] initWithFrame:rightBtn.bounds];

        rightBtnLabel.textAlignment = NSTextAlignmentCenter;

        rightBtnLabel.text = rightTitle;

        [rightBtn addSubview:rightBtnLabel];

        [rightBtnLabel release];

        rightBtnLabel.userInteractionEnabled = NO;

    }

    returnself;

}


@end


二、导航条左右按钮是图片形式——图片上有文字


1.NavView.h文件

#import <UIKit/UIKit.h>


@interface NavView : UIView


-(id)initWithFrame:(CGRect)rect andTitle:(NSString*)title andLeftBtnImage:(NSArray*)leftImage andleftBtnSelector:(SEL)leftSelector andRightBtnImage:(NSArray*)rightImage andRightBtnSelector:(SEL)rightSelector andTarget:(id)target;


@end



2.NavView.m文件


#import "NavView.h"


@implementation NavView

//因为这个类是继承于UIView的子类,所以这个类也是一个View,所以在这个类力,self就表示这个类所代表的View

-(id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if(self)

    {

        

    }

    returnself;

}//UIView的子类的主方法就是init方法


-(id)initWithFrame:(CGRect)rect andTitle:(NSString *)title andLeftBtnImage:(NSArray *)leftImage andleftBtnSelector:(SEL)leftSelector andRightBtnImage:(NSArray *)rightImage andRightBtnSelector:(SEL)rightSelector andTarget:(id)target

{

    self = [super initWithFrame:rect];

    if(self)

    {

        //在这里调用下面的布局方法,把init方法中的参数当做下面布局方法的参数

        

        [self makeUIWithTitle:title andLeftBtnImage:leftImage andleftBtnSelector:leftSelector andRightBtnImage:rightImage andRightBtnSelector:rightSelector andTarget:target];

    }

    returnself;

}


//下面是布局的方法

-(void)makeUIWithTitle:(NSString*)title andLeftBtnImage:(NSArray*)leftImage andleftBtnSelector:(SEL)leftSelector andRightBtnImage:(NSArray*)rightImage andRightBtnSelector:(SEL)rightSelector andTarget:(id)target

{

    //导航条

    UIImageView *navView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320,44)];

    [self addSubview:navView];

    navView.image = [UIImage imageNamed:@"title_bg.png"];

    [navView release];

    //title

    UILabel *navLabel = [[UILabel alloc] initWithFrame:self.bounds];

    navLabel.text = title;//根据传过来的参数赋值

    navLabel.textColor = [UIColor whiteColor];

    navLabel.textAlignment = NSTextAlignmentCenter;

    navLabel.font = [UIFont boldSystemFontOfSize:19];

    [self addSubview:navLabel];

    [navLabel release];

    //左按钮

    for (int i=0; i<leftImage.count; i++) {

        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        leftBtn.frame = CGRectMake(10+i*48,10, 43,24);

        [leftBtn setImage:[UIImage imageNamed:leftImage[i]] forState:UIControlStateNormal];

        [leftBtn addTarget:target action:leftSelector forControlEvents:UIControlEventTouchUpInside];

        if(leftImage)

        {

            [self addSubview:leftBtn];

        }

    }

    //右按钮

    for (int i=0; i<rightImage.count; i++) {

        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        rightBtn.frame = CGRectMake(self.frame.size.width-35-i*35,7, 30,30);

        [rightBtn setImage:[UIImage imageNamed:rightImage[i]] forState:UIControlStateNormal];

        [rightBtn addTarget:target action:rightSelector forControlEvents:UIControlEventTouchUpInside];

        rightBtn.tag = 2000+i;

        if(rightImage)

        {

            [self addSubview:rightBtn];

        }

    }

    

}

@end



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值