SwiftUI MacOS项目根据屏幕大小调整窗口大小

代码实现

获取屏幕对象

1
var window = NSScreen.main?.visibleFrame

设置大小

1
2
3
4
HStack {
  
}
.frame(width: window!.width / 2.0, height: window!.height / 1.5)

汇总

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
struct Home: View {

	var window = NSScreen.main?.visibleFrame
  
  var body: some View {
    HStack {
      Text("Hello, World!")
    }
    .frame(width: window!.width / 2.0, height: window!.height / 1.5)
  }
}
Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy