hunter-ji/Blog

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

hunter-ji opened this issue · 0 comments

一. 代码实现

1. 获取屏幕对象

var window = NSScreen.main?.visibleFrame

2. 设置大小

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

二. 汇总

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)
  }
}