Ios SwiftUI以多画面形式获取屏幕大小
实际上,我在我的Ios SwiftUI以多画面形式获取屏幕大小,ios,swiftui,Ios,Swiftui,实际上,我在我的ContentView.swift文件中使用了 UIScreen.main.bounds.size.width 获取屏幕的宽度大小。不幸的是,我不能将其用于MacOS目标,因为它需要NSScreen NSScreen.main.bounds.size.width 是否有任何通用的Screen来获取屏幕大小?我所做的就是创建一个带有平台条件的类来解决这个问题 class SGConvenience{ #if os(watchOS) static var dev
ContentView.swift
文件中使用了
UIScreen.main.bounds.size.width
获取屏幕的宽度大小。不幸的是,我不能将其用于MacOS目标,因为它需要NSScreen
NSScreen.main.bounds.size.width
是否有任何通用的
Screen
来获取屏幕大小?我所做的就是创建一个带有平台条件的类来解决这个问题
class SGConvenience{
#if os(watchOS)
static var deviceWidth:CGFloat = WKInterfaceDevice.current().screenBounds.size.width
#elseif os(iOS)
static var deviceWidth:CGFloat = UIScreen.main.bounds.size.width
#elseif os(macOS)
static var deviceWidth:CGFloat? = NSScreen.main?.visibleFrame.size.width // You could implement this to force a CGFloat and get the full device screen size width regardless of the window size with .frame.size.width
#endif
}
然后,我只需在需要时调用sgconcility.deviceWidth您试用过GeometryReader吗?我没有给你屏幕的大小,但它确实给了你视图的最大大小。