iOS UINavigationController和设置导航条的内容

UINavigationController

  • UINavigationController以栈的形式保存子控制器
    • 栈的形式是先进后出,所以说我们一般只会看到栈顶的东西,剩下的东西都在栈顶下面,并且第一入栈的在栈的第最底部。
  • //使用push方法能将某个控制器压入栈 -(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

  • //使用pop方法可以移除控制器
    -(UIViewController *)popViewControllerAnimated:(BOOL)animated;

  • //回到根控制器(栈底控制器) -(NSArray *)popToRootViewControllerAnimated:(BOOL)animated;

设置导航条的内容

 //设置导航条内容
    self.navigationItem.title = @"根导航控制器";
    //设置导航条左边的按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"⬅️" style:UIBarButtonItemStyleDone target:self action:@selector(leftBtn)];
    //设置导航条的view(可自定义)
//    self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeInfoLight];
    
    /*
     UINavigationItem:控制导航条的内容
     UIBarButtonItem:控制导航条上的按钮内容
     */
    
    //在ios7以后默认导航条上的按钮会渲染成蓝色,可以通过代码告诉系统按钮不要渲染图片
    UIImage *image = [UIImage imageNamed:@"navigationbar_friendsearch"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(righthBtn)];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"navigationbar_friendsearch"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"navigationbar_friendsearch_highlighted"] forState:UIControlStateHighlighted];
    //导航条上的内容位置不能由开发者决定,开发者只能控制尺寸大小
    button.frame = CGRectMake(500, 500, 30, 30);
    //控件的尺寸由图片决定
    [button sizeToFit];
    
    //设置导航条右边的按钮为自定义控件
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
}

设置导航条内容demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容