UINavigationController--导航控制器(UINavigationBar、UIToolBar)

本文深入探讨iOS应用开发过程中的关键技术和Swift编程语言的高效用法,包括UI设计、性能优化、错误处理及第三方库集成等核心内容,旨在帮助开发者提升iOS应用的开发效率与质量。

UINavigationController

导航控制器:

       一个导航控制器,管理着一个数组Array,可以调用-(void)viewControllers方法获得这个数字,这个数组中存放的是一个一个的ViewController但是管理这个数组所使用的方法,是栈的方法。

压栈:  [self.navigationController pushViewController:secondVC animated:YES];

弹栈: [self.navigationController popToRootViewControllerAnimated:YES];

  //调转到指定的莫一页

  [self.navigationController popToViewController:secondVC animated:YES];


      

       一个导航条(UINavigationBar),管理着一个数组,这个数组是存放是UINavigationItem,也是使用栈的结构。

       UINavigationItem的结构一般是:左键+标题+右键)

     

       一个工具栏,是一排的UIBarButtonItem按键,注意:toolbar并不像navigationBar,是在所有viewControllers中的viewController显示,只能每个ViewController设置自己的toolbar,但是实际开发,一般不用toolbar,因为,toolbar是只是显示一排按键,我们可以使用自己定制的UIView来实现显示一排按键,而且可控性更强。

     

       一个导航控制器自带有一个导航条和一个工具栏,一个ViewController自带一个UINavigationItemtoolbarItems

     

       在项目实际开发中,结合实际情况,一般使用UINavigationControllerUITabBarController来管理多个viewcontroller之间的切换

     

 AppDelegate中:

    

    AZRootViewController *rootVc=[[AZRootViewControlleralloc] init];

    UINavigationController *nav=[[UINavigationControlleralloc] initWithRootViewController:rootVc];

    [nav.navigationBarsetBackgroundColor:[UIColororangeColor]];

    

    //可以隐藏导航条,yes表示隐藏,no表示不隐藏,默认不隐藏

    

    nav.navigationBarHidden=NO;


    //ios7以后,导航条的位置将通知栏都覆盖了,但是为了兼容以前的版本,我们可以关掉这个

    nav.edgesForExtendedLayout=UIRectEdgeTop;

    

    

    //toolbar,工具栏,显示工具栏,默认不显示

//    nav.toolbarHidden=NO;

    [nav setToolbarHidden:NOanimated:NO];

    

    self.window.rootViewController=nav;


 AZRootViewController.m:


- (void)viewDidLoad

{

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    //一个ViewController自带有一个UINavigateItem

    

    self.title=@"首页";//其实就是使用自带的UINavigateItem中的标题属性

    self.navigationItem.title=@"首页";//这两种方法实际上时等同的。

    self.view.backgroundColor=[UIColorredColor];

   self.navigationItem.rightBarButtonItem=[[UIBarButtonItemalloc] initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(btnClick)];

    

//    //如果希望每个界面下面显示的工具栏不一样的话,那么可以直接到每一个ViewController中进行设置自己的toolbaritems

    

    NSMutableArray *array=[NSMutableArrayarray];

   

        //创建toolbar按键

   

    //先创建空白按键,用于隔开其他按键

    UIBarButtonItem *spaceBtn=[[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:selfaction:nil];

    [arrayaddObject:spaceBtn];

    

    

    UIBarButtonItem *btn1=[[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:selfaction:nil];

    [btn1setTitle:@"+"];

    [arrayaddObject:btn1];

    [arrayaddObject:spaceBtn];

    

    UIBarButtonItem *btn2=[[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarkstarget:selfaction:nil];

    [btn2setTitle:@"查看"];

    [arrayaddObject:btn2];

    [arrayaddObject:spaceBtn];

    

    

    UIBarButtonItem *btn3=[[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearchtarget:selfaction:nil];

    [btn3setTitle:@""];

    [arrayaddObject:btn3];

    [arrayaddObject:spaceBtn];

   

   self.toolbarItems=array;

   

}

-(void)btnClick

{

    secondVC=[[AZSecondViewControlleralloc] init];

    [self.navigationControllerpushViewController:secondVCanimated:YES];

}


AZSecondViewController.m:



- (void)viewDidLoad

{

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    self.navigationItem.title=@"第二页";

   self.navigationItem.rightBarButtonItem=[[UIBarButtonItemalloc] initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(btnClick)];

    

}

-(void)btnClick

{

    thirdVC=[[AZThirdViewControlleralloc] init];

    [self.navigationControllerpushViewController:thirdVCanimated:YES];

}



AZThirdViewController.m:


- (void)viewDidLoad

{

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    //可以隐藏返回按键,yes表示隐藏,no表示不隐藏,默认不隐藏

    [self.navigationItemsetHidesBackButton:YES];

    

    self.navigationItem.title=@"未页";

    self.navigationItem.rightBarButtonItem=[[UIBarButtonItemalloc] initWithTitle:@"首页"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(btnClick)];

    

}

-(void)btnClick

{

    //直接回到首页

    rootVC=[[AZRootViewControlleralloc] init];

    [self.navigationControllerpopToRootViewControllerAnimated:YES];

//    

//    secondVC=[[AZSecondViewController alloc] init];

//    //调转到指定的莫一页

//    [self.navigationController popToViewController:secondVC animated:YES];

}


UINavigationBar 导航条:


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

    

    

    //UINavigationBar 是属于view

    

    _navigationBar=[[UINavigationBar alloc] initWithFrame:CGRectMake(10, 20, 300, 40)];

    [self.view addSubview:_navigationBar];

    

    

    //设置导航条的半透明效果,yes表示半透明,no表示不透明

    _navigationBar.translucent=YES;

    

    //设置渲染颜色

    _navigationBar.tintColor=[UIColor orangeColor];

    

    //设置背景颜色

    _navigationBar.backgroundColor=[UIColor greenColor];

    

    //设置背景图片

    [_navigationBar setBackgroundImage:[UIImage imageNamed:@"top.png"] forBarMetrics:UIBarMetricsDefault];

    //UINavigationItem 是属于model

    UINavigationItem *item=[[UINavigationItem alloc] initWithTitle:@"主页"];

    item.title=@"首页";

    

    //但是titile这种属性,只能显示默认的字体,颜色,大小。如果我们想自定制字体大小,颜色,可以使用titleView

    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];

    label.text=@"居中";

    label.textAlignment=NSTextAlignmentCenter;

    label.textColor=[UIColor redColor];

    label.font=[UIFont boldSystemFontOfSize:20];

    item.titleView=label;

    

    //右边按键

    //注意:导航条上得buttonUIBarButtonItem类型

    UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(BtnClick:)];

    rightBtn.tag=100;

    item.rightBarButtonItem=rightBtn;

    

    [_navigationBar pushNavigationItem:item animated:NO];

    

    

    

}


-(void)BtnClick:(id)sender

{

    UIBarButtonItem *btn=(UIBarButtonItem *)sender;

    if (btn.tag==100)

    {

         UINavigationItem *item=[[UINavigationItem alloc] initWithTitle:@"第二页"];

        

        //使用title设置自己的返回按键

//        UIBarButtonItem *leftBtn=[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(BtnClick:)];

        

        //使用image设置自己的返回按键

        

        //关闭图片的渲染模式

        UIImage *leftImage=[UIImage imageNamed:@"leftBtn.png"];

        [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

        

        UIBarButtonItem *leftBtn=[[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(BtnClick:)];

        leftBtn.tag=200;

        item.leftBarButtonItem=leftBtn;

        

        [_navigationBar pushNavigationItem:item animated:YES];

    }

    if (btn.tag==200)

    {

        [_navigationBar popNavigationItemAnimated:YES];

    }

   

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值