自定义navigation

来源:互联网 发布:网络赌钱游戏网站 编辑:程序博客网 时间:2024/05/22 03:05

几乎任何一个项目都会用到navigation  故小桐在这里写一个简单的自定义navigationDemo  新人看看就可  望大神一笑而过~


.h文件:

这里声明三个方法 一个是设置 navigation的title 一个是左侧的返回 右侧的设置

-(void)setDQTitle:(NSString*)title;


- (void)setFanhui;

- (void)setShezhi;


.m文件:

- (void)viewDidLoad

{

    [superviewDidLoad];

    // Do any additional setup after loading the view.

     [self.navigationController.navigationBarsetBackgroundImage:[UIImageimageNamed:@"daohangtiao.png"]forBarMetrics:UIBarMetricsDefault];

}


//navigation标题

-(void)setDQTitle:(NSString*)title{

    

   NSArray *titleArray=[titlecomponentsSeparatedByString:@""];

   if (titleArray.count) {

       UIView *vc=[[UIViewalloc]initWithFrame:CGRectMake(0,0,220,44)];

        vc.backgroundColor=[UIColorclearColor];

        

       UILabel *upLabel=[[UILabelalloc]initWithFrame:CGRectMake(-10,10,220,20)];

        upLabel.textColor=[UIColorwhiteColor];

        upLabel.font=[UIFontboldSystemFontOfSize:19];

        upLabel.backgroundColor=[UIColorclearColor];

        upLabel.textAlignment=NSTextAlignmentCenter;

        [vcaddSubview:upLabel];

        upLabel.text=[titleArrayobjectAtIndex:0];

        self.navigationItem.titleView=vc;

    }

}


//navigation返回

- (void) setFanhui

{

    UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];

    btn.frame =CGRectMake(0,0,11,20);

    [btn setImage:[UIImageimageNamed:@"btn_fanhui@2x.png"]forState:UIControlStateNormal];

    [btn addTarget:selfaction:@selector(fanhuiBtn)forControlEvents:UIControlEventTouchUpInside];;

    UIBarButtonItem *leftBtn = [[UIBarButtonItemalloc]init];

    [leftBtnsetCustomView:btn];

    

    self.navigationItem.leftBarButtonItem = leftBtn;

}


//navigation右侧按钮

- (void)setShezhi

{

    UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];

    btn.frame =CGRectMake(100,0,48,26);

    [btn setImage:[UIImageimageNamed:@"btn_fasong.png"]forState:UIControlStateNormal];

    //点击事件

    //[btn addTarget:self action:@selector(fanhuiBtn) forControlEvents:UIControlEventTouchUpInside];;

    UIBarButtonItem *rightBtn = [[UIBarButtonItemalloc]init];

    [rightBtnsetCustomView:btn];

    

    self.navigationItem.rightBarButtonItem = rightBtn;

}


在哪个界面用 调用

[selfsetDQTitle:@"标题"];

 //导航左侧按钮

 [selfsetFanhui];

//导航右侧按钮

  [selfsetShezhi];



0 0