IOS学习之NavigationBar(标题栏)

2016.10.19
特别说明:本标题栏样式部分参照github上的bacy/titlebar
PS:谢谢网上诸多大神资料

效果图:关注标题就可以啦

IOS学习之NavigationBar(标题栏)_第1张图片
Simulator Screen Shot 2016年10月19日 下午6.36.57.png
  • 对象的定义
@interface ViewController ()

//需要操作的按键需要定义出来

@property (strong, nonatomic) UIBarButtonItem *leftBtn;

@property (strong, nonatomic) UIBarButtonItem *rightBtn2;

@property (strong, nonatomic) UIBarButtonItem *rightBtn;

@end

  • 定义一个函数,专门用来生成标题栏
-(void)NavigationBarInit{

    //创建一个导航栏
    UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44+20)];
    
    //标题栏的整个背景色改为蓝色
    [navigationBar setBarTintColor:[UIColor colorWithRed:64/255.0 green:180/255.0 blue:255/255.0 alpha:1.0]];
    
    //将标题栏中间的主title的文字颜色设置为白色
    [navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
    
    //此段设置整个标题栏的背景图片
//    [navigationBar setBackgroundImage:[UIImage imageNamed:@"xx"] forBarMetrics:UIBarMetricsDefault];
    
    //设置左右两边的图片和文字均为白色
    [navigationBar setTintColor:[UIColor whiteColor]];
   
    //将导航栏添加入view中
    [self.view addSubview:navigationBar];
   
    //新建一个navigationItem
    UINavigationItem *navigationItem = [[UINavigationItem alloc] init];
    
    //title为中间的显示,可以是view,可以是文字等等
//    navigationItem.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
//    navigationItem.titleView.backgroundColor = [UIColor yellowColor];
    
    //标题栏中间的文字
    navigationItem.title = @"主菜单";
    
    //标题栏左侧一个按钮+文字
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
  
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoLight];
    
    //btn.backgroundColor=[UIColor clearColor];
    btn.frame = view.frame;
    
    [btn setImage:[UIImage imageNamed:@"back_small"] forState:UIControlStateNormal];
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    
    [btn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [view addSubview:btn];
    
    _leftBtn = [[UIBarButtonItem alloc]initWithCustomView:view];
    
    navigationItem.leftBarButtonItem = _leftBtn;
    
    //标题栏右侧,两个按钮
    
    UIButton *btnR1 = [UIButton buttonWithType:UIButtonTypeInfoLight];
    UIButton *btnR2 = [UIButton buttonWithType:UIButtonTypeInfoLight];
    
    btnR1.frame = CGRectMake(0, 0, 20, 20);
    btnR2.frame = CGRectMake(0, 0, 20, 20);
    
    [btnR1 setImage:[UIImage imageNamed:@"fabu"] forState:UIControlStateNormal];
    [btnR2 setImage:[UIImage imageNamed:@"collect"] forState:UIControlStateNormal];

    _rightBtn = [[UIBarButtonItem alloc] initWithCustomView:btnR1];
    _rightBtn2 = [[UIBarButtonItem alloc] initWithCustomView:btnR2];
    
    navigationItem.rightBarButtonItems=@[_rightBtn,_rightBtn2];
    
    [navigationBar pushNavigationItem:navigationItem animated:YES];
    
}
  • 最后在viewDidLoad调用NavigationBarInit
- (void)viewDidLoad {
    [super viewDidLoad];
    //调用
    [self NavigationBarInit];
}

你可能感兴趣的:(IOS学习之NavigationBar(标题栏))