iOS之导航栏基本设置

1.先看下导航栏的构造

2.在storyboard中创建导航栏

   选中View Controller (Main.storyboard中)

3.设置导航栏(用代码)

1 //设置背景颜色
2     [self.navigationController.navigationBar setBackgroundColor:[UIColor orangeColor]];
3     
4     //更改导航栏的背景图片 或者背景颜色
5     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavigationBar_BG"] forBarMetrics:UIBarMetricsDefault];
1 //创建一个拥有图片的按钮
2     UIImage *img = [[UIImage imageNamed:@"NavigationBar_Btn_Back"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
3     UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(back)];//back为自己定义的事件
4     self.navigationItem.leftBarButtonItem = back;
//自定义一个按钮 添加到导航栏的右边
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 60, 35);//图片资源高度一般就是这里的高度
    [btn setBackgroundImage:[[UIImage imageNamed:@"NavigationBar_Btn"]stretchableImageWithLeftCapWidth:15/2 topCapHeight:35/21] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
    //创建UIBarButtonItem
    UIBarButtonItem *nextBtn = [[UIBarButtonItem alloc]initWithCustomView:btn];
    
    self.navigationItem.rightBarButtonItem = nextBtn;
//中间的视图 -> 显示标题
    self.title = @"标题";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
     //显示图片 按钮 UIView
    UIImageView *logo = [[UIImageView alloc]initWithFrame:CGRectMake(0, 34, 120, 19)];
    logo.image = [UIImage imageNamed: @"NavigationBar_Logo"];
    
    self.navigationItem.titleView = logo;
  //默认工具条UIToorBar是隐藏的
    self.navigationController.toolbarHidden = NO;//不让它隐藏
    
    //添加按钮 UIBarButtonItem
    //创建用于均分toolBar的barButtonItem
    UIBarButtonItem *flexible = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIBarButtonItem *copyBtn = [[UIBarButtonItem alloc]initWithTitle:@"Copy" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    //将按钮添加到toolBar上
    self.toolbarItems = @[flexible,copyBtn,flexible];

猜你喜欢

转载自www.cnblogs.com/frosting/p/9483078.html