美文网首页
导航栏和状态栏

导航栏和状态栏

作者: DDY | 来源:发表于2016-11-14 12:25 被阅读82次
    NavgationBar.png

    标题/隐藏BottomBar

    Test1 *vc = [[Test1 alloc] init];
    vc.title = @"Test1";
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:NO];
    

    navigationItem

    • 返回样式:系统图标,文字自定义
    // 父控制器,push方法前
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    // 颜色,会影响后面界面
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    
    • 返回样式:自定义图标和文字
    // 父控制器,push方法前
    [self.navigationController.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"navBack"]];
    [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"navBack"]];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"自定义图标" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backItem;
    
    • 返回样式:无图标,自定义文字
    // 当前控制器
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"BackToRooVC" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    • rightBarButtonItem
    // 当前控制器
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];
    
    • 隐藏返回按钮
    [self.navigationItem setHidesBackButton:YES];
    

    statusBar

    • iOS 7 以前(已成历史,不做赘述)
    StatusBar.png
    • iOS 7 - iOS 9

    用UIApplication的API
    info.plist里将View controller-based status bar appearance 设为NO,默认YES

    // 无动画效果
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault]; 
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    
    // 带动画效果
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    

    重写ViewController方法
    info.plist里将View controller-based status bar appearance 设为YES,默认YES

    - (void)changeStatusBarStyle:(UIButton *)sender {
        if (sender.tag == 100) {
            _statusBarStyle = UIStatusBarStyleDefault;
        } else {
            _statusBarStyle = UIStatusBarStyleLightContent;
        }
        [self setNeedsStatusBarAppearanceUpdate];
    }
    
    - (void)hiddenStatusBar:(BOOL)sender {
        _statusBarHidden = sender;
        [self setNeedsStatusBarAppearanceUpdate];
    }
    
    #pragma mark - 需要重写的几个状态栏方法
    #pragma mark 样式
    - (UIStatusBarStyle)preferredStatusBarStyle {
        return _statusBarStyle;
    }
    #pragma mark 显隐性
    - (BOOL)prefersStatusBarHidden {
        return _statusBarHidden;
    }
    #pragma mark 显/隐切换动画
    - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
        return UIStatusBarAnimationSlide;
    }
    
    • iOS 9 后

    用UIApplication的API 会报错

    <Error>: CGContextSaveGState:
    <Error>: CGContextTranslateCTM:
    <Error>: CGContextRestoreGState:
    

    苹果推荐重写ViewController方法

    • 上述方法失效
    if (statusBarStyle == UIStatusBarStyleLightContent)
    {
        weakSelf.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    }
    else
     {
          weakSelf.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    }
    

    状态栏高度和导航栏高度

    状态栏通常高度20,在通话中和有热点连接时40

    • 获取状态栏高度
      [[UIApplication sharedApplication] statusBarFrame].size.height

    • 状态栏发生变化通知

    UIApplicationWillChangeStatusBarFrameNotification
    UIApplicationDidChangeStatusBarFrameNotification
    
    • 获取导航栏高度
      self.navigationController.navigationBar.frame.size.height

    去除导航栏分割线

    • 第一种方式

    直接设置navigationBar的ShadowImage和BackgroundImag,这样做可以一劳永逸,在当前导航控制器的所有子控制器页面看到的导航栏都是没有分割线的。
    弊端:想恢复比较麻烦

    // 分割线
    [self.navigationController.navigationBar setShadowImage:[UIImage new]]; 
    // 背景图片
     // [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    
    • 第二种方式

    取出分割线,控制显隐性即可

    UIView *backgroundView = [self.navigationController.navigationBar subviews].firstObject;
    for (UIImageView)
    //_navLine = backgroundView.subviews.lastObject;
    for (UIView *view in backgroundView.subviews) {
            if ([view isKindOfClass:[UIImageView class]] && view.ddy_h <= 0.5) {
                _navLine = (UIImageView *)view;
            }
        }
    _navLine.hidden = YES;
    
    • 其他方式:完全递归遍历视图取得(太麻烦)

    改变导航栏背景

    // 颜色
    self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
    // 图片[self.navigationController.navigationBar setBackgroundImage: [UIImage imageNamed:@"banner.png"] forBarMetrics:UIBarMetricsDefault];
    // 字体
    [self.navigationController.navigationBar.titleTextAttributes=@{NSForegroundColorAttributeName:[UIColorwhiteColor],NSFontAttributeName:[UIFontboldSystemFontOfSize:17]};
    
    作者:兜麦
    链接:http://www.jianshu.com/p/58e3cb268f8e
    來源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    
    // 如果想透明,设置颜色透明不行,因为导航上有一张背景img
    barImageView = self.navigationController.navigationBar.subviews.firstObject;
    //提前给barImageView一个背景色(给barImageView设置一个图片也行)
    barImageView.backgroundColor = [UIColor greenColor];
    
    

    按钮位置调整

    Image1.png Image2.png
    self.navigationItem.leftBarButtonItem.imageInsets = UIEdgeInsetsMake(0,-20,0,0);
    self.navigationItem.rightBarButtonItem.imageInsets = UIEdgeInsetsMake(0,-10,0,10);
    

    Status Bar变换
    iOS 关于navigationBar的一些:毛玻璃、透明、动态缩放、动态隐藏
    ttp://www.jianshu.com/p/8c8303f7d439
    让我们一次性解决导航栏的所有问题;

    相关文章

      网友评论

          本文标题:导航栏和状态栏

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