Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios SwiftUI以多画面形式获取屏幕大小_Ios_Swiftui - Fatal编程技术网

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吗?我没有给你屏幕的大小,但它确实给了你视图的最大大小。