美文网首页
iOS UINavigationController和设置导航条

iOS UINavigationController和设置导航条

作者: 哔哩哔哩智能喵 | 来源:发表于2016-10-10 18:25 被阅读553次

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

相关文章

网友评论

      本文标题:iOS UINavigationController和设置导航条

      本文链接:https://www.haomeiwen.com/subject/zglvyttx.html