Created
June 11, 2023 10:06
-
-
Save MoussaHellal/ee4f947cbed1d7e43ce8eeda17935825 to your computer and use it in GitHub Desktop.
Getting SafeArea, TabBar Sizes in SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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