IOS15的导航栏高度和状态栏高度和tabBar高度的获取

IOS15的导航栏高度和状态栏高度和tabBar高度的获取

这都2021年11月份了。还在有人使用[UIApplication sharedApplication].keyWindow
这样过期的api?。虽然能用,但是报警告。苹果提供了UIWindowScene这个类。这部分属于冷门知识,资料很少。swift的写法还是有的。swift其实跟oc一个道理,就是换种写法而已,本质上没变

-(void)setupTitleView{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 44+47, kScreenW, 44)];
    titleView.backgroundColor = UIColor.systemGray4Color;
    [self.view addSubview:titleView];
    NSLog(@"navigationBar %@",NSStringFromCGRect(self.navigationController.navigationBar.frame));
  //  NSLog(@"%@",NSStringFromCGRect([[UIApplication sharedApplication] statusBarFrame]));
    NSSet *set = [[UIApplication sharedApplication] connectedScenes];
    UIWindowScene *windowScene = [set anyObject];
    UIStatusBarManager *statusBarManager2 =  windowScene.statusBarManager;
    NSLog(@"statusBarFrame %@",NSStringFromCGRect(statusBarManager2.statusBarFrame));
    NSLog(@"tabBar %@",NSStringFromCGRect(self.tabBarController.tabBar.frame));
}

打印日志:

2021-11-28 19:59:01.213666+0800 testDouyin[21879:437391] navigationBar {{0, 0}, {390, 44}}
2021-11-28 19:59:01.213761+0800 testDouyin[21879:437391] statusBarFrame {{0, 0}, {390, 47}}
2021-11-28 19:59:01.213870+0800 testDouyin[21879:437391] tabBar {{0, 761}, {390, 83}}

打印则证明了状态栏高度47.导航栏高度44.TabBar高度83,以上是在iPhone13 模拟机测试的。对于没有刘海屏幕的iPhone,应该不一样,需要我们动态的获取。而不是写s。

swift写法:

  //过期的函数
   //    let h =  UIApplication.shared.statusBarFrame.size.height
        let set = UIApplication.shared.connectedScenes
        let sc =  set.first
        guard let windowScene =  (sc as? UIWindowScene) else { return }
        guard  let  statusBarManager = windowScene.statusBarManager else { return }
        let frame = statusBarManager.statusBarFrame
        let h1 = frame.height

你可能感兴趣的:(IOS,xcode,swift,ios)