1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

iOS強化月間 - iOSアプリ開発の知見を共有しよう -

【SwiftUI】positionで画面の隅に図形を移動させたい

Posted at

はじめに

positionで画面の隅に図形を移動させたい事があり、苦戦したので記録しておきます。

こんな感じ

Simulator Screenshot - iPhone SE (3rd generation) - 2023-09-12 at 20.57.12.png

実装

import SwiftUI

struct ContentView: View {
+   private var rect: CGRect {
+       let scenes = UIApplication.shared.connectedScenes
+       let windowScene = scenes.first as? UIWindowScene
+       return windowScene?.windows.first?.screen.bounds ?? .init()
+   }
    
+   private var safeareaInsets: UIEdgeInsets {
+       let scenes = UIApplication.shared.connectedScenes
+       let windowScene = scenes.first as? UIWindowScene
+       return windowScene?.windows.first?.safeAreaInsets ?? .init()
+   }

    var body: some View {
        VStack {
            Text("テスト")
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .padding(20)
        .background(content: backgroundView)
    }
    
    private func backgroundView() -> some View {
        ZStack {
            Circle()
                .frame(width: 30, height: 30)
                .foregroundColor(.blue)
+               .position(x: rect.minX, y: rect.minY - safeareaInsets.top)
            
            Circle()
                .frame(width: 30, height: 30)
                .foregroundColor(.blue)
+               .position(x: rect.maxX, y: rect.maxY - safeareaInsets.top - safeareaInsets.bottom)
        }
    }
}

おわり

どうなってるのか分からずめっちゃ時間かかりました。

1
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?