LoginSignup
1
3

More than 1 year has passed since last update.

【SwiftUI】macOSでウインドウサイズを固定する

Posted at

はじめに

frame(minWidth:idealWidth:maxWidth:minHeight:idealHeight:maxHeight:alignment:)を設定することでウインドウサイズを固定できると思っていたらできなくてつまづいたので記録しておきます。

私の認識では以下のようにすれば1番大きい時で110、1番小さい時で100になると思ってました。

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
        }
        .frame(minWidth: 100, maxWidth: 110, minHeight: 100, maxHeight: 110)
    }
}

しかし、実際は以下のようになります。
画面収録_2023-01-28_20_53_28_AdobeExpress.gif

frameが機能していません

やりかた

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
        }
        .frame(minWidth: 100, maxWidth: 110, minHeight: 100, maxHeight: 110)
    }
}
import SwiftUI

@main
struct macos_window_size_sampleApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
+       .windowResizability(.contentSize)
    }
}

おわり

最近macOSアプリを作ることが多いので新たな発見があって楽しいです

参考記事

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3