美文网首页
iOS 导航栏基础设置

iOS 导航栏基础设置

作者: LYPC_下里巴人 | 来源:发表于2017-12-15 13:39 被阅读31次
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.title = @"测试";
        // 修改 状态栏+导航栏背景色
        [self.navigationController.navigationBar setBarTintColor:[UIColor yellowColor]];
        // 导航栏文字颜色+字号
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18 weight:1], NSForegroundColorAttributeName:[UIColor greenColor]}];
        // 设置导航栏的背景图片
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"111"] forBarMetrics:UIBarMetricsDefault];
        // 自定义状态栏背景view 一般不会在这里做手脚,但是也不排除特殊情况使用,原因是由于状态栏区域上的控件是隐藏的,所以只要在状态栏区域被渲染了颜色,状态栏的背景颜色就跟着一起改变,从而改变了状态栏的背景
        UIView *statusBarView = [[UIView alloc]   initWithFrame:CGRectMake(0, -20,    self.view.bounds.size.width, 20)];
        statusBarView.backgroundColor = [UIColor purpleColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 50, 20)];
        label.backgroundColor = [UIColor orangeColor];
        label.textColor = [UIColor whiteColor];
        label.text = @"广告";
        [statusBarView addSubview:label];
        [self.navigationController.navigationBar addSubview:statusBarView];
        // 设置原点从导航栏之下开始 默认全屏
    //    self.edgesForExtendedLayout = 0;
        /* 设置状态栏的样式:白色字 、黑色字
    以前这样:
         在plist文件添加字段:View controller-based status bar appearance,并设置BOOL值为NO,
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
         */
    最新的设置补充在最底部
        
        // 设置导航栏为透明,返回按钮和导航栏上的按钮标题控件依旧展示:注释掉self.edgesForExtendedLayout = 0; 不要对状态栏做自定义View
        [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.shadowImage = [UIImage new];
    
        
        self.view.backgroundColor = [UIColor redColor];
        
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    https://juejin.im/post/5a335dd751882506683cdbeb
    

    最新的设置状态栏为白色的方法
    info.plist文件里面:View controller-based status bar appearance设置为NO
    然后在controller基类里面:

    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
        self.navigationController.navigationBarHidden = YES;
    }
    
    - (void)viewDidAppear:(BOOL)animated{
        [super viewDidAppear:animated];
        if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
            [self prefersStatusBarHidden];
            [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
        }
    }
    
    -(UIStatusBarStyle)preferredStatusBarStyle{
        ///这里设置白色
        return UIStatusBarStyleLightContent;
    }
    
    -(BOOL)prefersStatusBarHidden{
        return NO;
    }
    

    相关文章

      网友评论

          本文标题:iOS 导航栏基础设置

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