状态栏消失的问题

2018-05-18  本文已影响16人  chulijun

webView加载h5网页视频,横屏后导致状态栏消失的问题。

问题说明:项目是不支持横屏的,但是接入的流量新闻是一个web 网页,网页中包含各种类型的广告,纯文本的,富文本的,视频类的。
在视频类的web界面中包含的有视频的横竖屏切换按钮(按钮在网页中),播放视频时,如果是竖屏状态下,点击视频关闭按钮,返回时,状态栏正常。如果在横屏状态下,点击视频关闭按钮的话,返回之后导航条上移20px,下方回留白20px。解决方法如下:

第一步:在自定义的BaseUINavViewController 中 添加如下代码:

- (void)viewDidLoad {
  [super viewDidLoad];
  //设置整个项目的item状态
  UIBarButtonItem *item = [UIBarButtonItem appearance];
  //设置item普通状态
  NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
  attrs[NSFontAttributeName] = [UIFont systemFontOfSize:16];
  attrs[NSForegroundColorAttributeName] =YTColor(@"#030303");
  [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
  self.navigationBar.barTintColor = [UIColer blueColer];
// 检测屏幕旋转的变化
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}

- (void)didRotate{
//改变导航栏位置
if(!UIDeviceOrientationIsPortrait(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))) {
      self.navigationBar.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64);
  }else{
      self.navigationBar.frame =CGRectMake(0, 20,  [UIScreen mainScreen].bounds.size.width, 44);
  }
  [[UIApplication sharedApplication]setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];//启动图遮盖状态栏,显示出状态栏
  [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; //状态栏字体设置 白色
  self.navigationController.navigationBar.barTintColor = YTBgTopColor;
}

第二步:在加载视频对应的controller 中添加如下代码,⚠️这里需要添加一个蓝色的图片

- (void)viewDidLoad {
    [super viewDidLoad];
    /*
     方法1:添加View,为VIew设置颜色,但是失败
     方法2:添加View,为UIImageView添加蓝色图片解决问题。
     是白发
     */
    UIView *statusBarView = [[UIView alloc]   initWithFrame:CGRectMake(0, -20,    self.view.bounds.size.width, 20)];
    //    statusBarView.backgroundColor = [UIColor blueColor];这里是管用的,但是找不到相同色系
    //    imageView.backgroundColor = [UIColor colorWithRed:59.0 green:165.0 blue:221.0 alpha:1];配置成相同的颜色了,但是又不能显示颜色了,原因未知。
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:statusBarView.bounds];
    imageView.image = [UIImage imageNamed:@"blue"];
    [statusBarView addSubview:imageView];
    [self.navigationController.navigationBar addSubview:statusBarView];
    self.automaticallyAdjustsScrollViewInsets =NO;
}

第三步,需要在info.plist 中添加配置

<!-- 设置状态栏 start -->
    <key>UIStatusBarHidden</key>
    <false/>
    <key>UIStatusBarStyle</key>
    <string>UIStatusBarStyleLightContent</string>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
    </array>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    <!-- 设置状态栏 end -->

后记:这是实验出来的结果,可能不是最优的方案,但是目前没有找到其他的方案,希望谁遇见相同问题的,并且知道更好的解决办法者,留言交流。非常感谢!

上一篇下一篇

猜你喜欢

热点阅读