UINavigationController

隐藏NavigationBar时的一个坑(很实用)
自定义iOS7导航栏背景,标题和返回按钮文字颜色
iOS手势返回的实现(自定义返回按钮)

  • 修改所在视图及下级视图的 tabBar 背景颜色
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
  • 设置自定义标题 Title
self.navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont(name: "Times new roman", size: 20)!]
  • 修改所在视图 除标题外的 文字颜色
 self.navigationController.navigationBar.tintColor = UIColor.whiteColor()
  • 透明度 Translucent
 navigationController?.navigationBar.translucent = false 
// 如果设置这个属性为 True, 同时设置了一个不透明的自定义背景图片,导航栏将使用一个小于1.0的系统不透明度给这个图片
// 如果在设置这个属性为 False, 同时设置了一个透明的自定义背景图片,导航栏将会用给图片提供一个黑色不透明背景如果你使用了UIBarStyleBlack, 提供白色不透明背景如果使用了UIBarStyleDefault, 或者设置为barTintColor 如果这个值有被设置的话
  • 修改下一视图的 返回按钮 的属性
let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
  self.navigationItem.backBarButtonItem = backButton
  • 导航栏样式 barStyle
 navigationController?.navigationBar.barStyle = .Black
    // 这个会导致navigationBar 变成黑色背景 
    // .Default 是白色背景黑色的字
  • 修改UINavigationController,UINavigationBar背景颜色,字体颜色
- (void)setNav
{
    UINavigationBar *bar = [UINavigationBar appearance];

    //设置显示的颜色
    bar.barTintColor = [UIColor colorWithRed:62/255.0 green:173/255.0 blue:176/255.0 alpha:1.0];

    //设置字体颜色
    bar.tintColor = [UIColor whiteColor];

    [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

//或者用这个都行
    [bar setTitleTextAttributes:@{UITextAttributeTextColor : [UIColor whiteColor]}];
UINavigationController_第1张图片
效果图

隐藏底部Bar

appDetaiVC.hidesBottomBarWhenPushed = YES;
NavigationController和ViewController 都可以调用该方法
自定制返回键

 //修改系统返回键是图片和文字,会保留侧拉返回功能
  UIImage *backButtonImage = [[UIImage imageNamed:@"fanhui.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
  [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  //将返回按钮的文字position设置不在屏幕上显示
  [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    // 开启
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

- (void)viewWillDisappear:(BOOL)animated { 
  [super viewWillDisappear:animated]; 
//代理置空,否则会闪退
  if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
 self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
  }
- (void)viewDidAppear:(BOOL)animated {
   [super viewDidAppear:animated];
   //开启iOS7的滑动返回效果
  if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) 
  { 
   //只有在二级页面生效 
   if ([self.navigationController.viewControllers count] == 2) { 
self.navigationController.interactivePopGestureRecognizer.delegate = self; 
  }
 }
}

你可能感兴趣的:(UINavigationController)