如何在Swift中以编程方式获取状态栏的高度?
在Objective-C中,它是这样的:
[UIApplication sharedApplication].statusBarFrame.size.height.
答案 0 :(得分:203)
Swift 2.x :
是否有任何问题UIApplication.sharedApplication().statusBarFrame.size.height
Swift 3或Swift 4 :
UIApplication.shared.statusBarFrame.height
确保导入UIKit
import UIKit
答案 1 :(得分:9)
更新后的答案支持Swift 5的iOS 13及更高版本的iOS
func getStatusBarHeight() -> CGFloat {
var statusBarHeight: CGFloat = 0
if #available(iOS 13.0, *) {
let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
statusBarHeight = window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
} else {
statusBarHeight = UIApplication.shared.statusBarFrame.height
}
return statusBarHeight
}
快乐编码!
答案 2 :(得分:4)
let app = UIApplication.sharedApplication()
let height = app.statusBarFrame.size.height
答案 3 :(得分:2)
就像在Objective-C中一样:
var screenStatusBarHeight: CGFloat {
return UIApplication.sharedApplication().statusBarFrame.height
}
这包含在以下标准变量中:
答案 4 :(得分:1)
易卜拉欣修改后的答案:
extension UIApplication {
static var statusBarHeight: CGFloat {
var statusBarHeight: CGFloat = 0
if #available(iOS 13.0, *) {
let window = shared.windows.filter { $0.isKeyWindow }.first
statusBarHeight = window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
} else {
statusBarHeight = shared.statusBarFrame.height
}
return statusBarHeight
}
}
答案 5 :(得分:0)
这是我使用的:
struct Screen {
static var width: CGFloat {
return UIScreen.main.bounds.width
}
static var height: CGFloat {
return UIScreen.main.bounds.height
}
static var statusBarHeight: CGFloat {
return UIApplication.shared.statusBarFrame.size.height
}
}
那么您可以做:
Screen.statusBarHeight
答案 6 :(得分:0)
在我的swiftUI项目中,此方法有效。
import UIKit
import SwiftUI
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = ContentView()
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
if let statusBarHeight = window.windowScene?.statusBarManager?.statusBarFrame.height {
SceneDelegateDataGetter.shared.height = statusBarHeight
}
window.rootViewController = HostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
class SceneDelegateDataGetter {
static let shared = SceneDelegateDataGetter()
public fileprivate(set) var height: CGFloat = 0
}
使用时,
SceneDelegateDataGetter.shared.height
答案 7 :(得分:0)
来自Peter Suwara 和Bobby 的回答
针对 Swift 5 进行了更新:
extension UIApplication {
static var statusBarHeight: CGFloat {
if #available(iOS 13.0, *) {
let window = shared.windows.filter { $0.isKeyWindow }.first
return window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
}
return shared.statusBarFrame.height
}
}
struct ScreenUtils {
static var width: CGFloat {
return UIScreen.main.bounds.width
}
static var height: CGFloat {
return UIScreen.main.bounds.height
}
static var statusBarHeight: CGFloat {
return UIApplication.statusBarHeight
}
}
使用:
ScreenUtils.statusBarHeight