Skip to content

Instantly share code, notes, and snippets.

@MoussaHellal
Created June 11, 2023 10:06
Show Gist options
  • Save MoussaHellal/ee4f947cbed1d7e43ce8eeda17935825 to your computer and use it in GitHub Desktop.
Save MoussaHellal/ee4f947cbed1d7e43ce8eeda17935825 to your computer and use it in GitHub Desktop.
Getting SafeArea, TabBar Sizes in SwiftUI
//
// SafeAreaTabBar+Extension.swift
// SafeAreTabBar
//
// Created by Moussa on 11/6/2023.
//
import SwiftUI
extension UITabBarController {
var height: CGFloat {
return self.tabBar.frame.size.height
}
var width: CGFloat {
return self.tabBar.frame.size.width
}
}
extension UIApplication {
var keyWindow: UIWindow? {
connectedScenes
.compactMap {
$0 as? UIWindowScene
}
.flatMap {
$0.windows
}
.first {
$0.isKeyWindow
}
}
}
private struct SafeAreaInsetsKey: EnvironmentKey {
static var defaultValue: EdgeInsets {
UIApplication.shared.keyWindow?.safeAreaInsets.swiftUiInsets ?? EdgeInsets()
}
}
private extension UIEdgeInsets {
var swiftUiInsets: EdgeInsets {
EdgeInsets(top: top, leading: left, bottom: bottom, trailing: right)
}
}
extension EnvironmentValues {
var safeAreaInsets: EdgeInsets {
self[SafeAreaInsetsKey.self]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment