Closed2

SwiftUIで画像の上に左上原点で描画

yorifujiyorifuji

ZStack(alignment: .topLeading) を指定してその中でImage()とオブジェクトを描画

import SwiftUI

struct ContentView: View {
    var body: some View {
        ZStack(alignment: .topLeading) {
            Image(systemName: "person")
                .resizable()
                .aspectRatio(contentMode: .fit)
            Rectangle()
                .foregroundColor(.blue)
                .frame(width: 100, height: 100)
                .opacity(0.8)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().previewLayout(.sizeThatFits)
    }
}

yorifujiyorifuji

offset()でオブジェクトの位置を指定する

struct ContentView: View {
    var body: some View {
        ZStack(alignment: .topLeading) {
            Image(systemName: "person")
                .resizable()
                .aspectRatio(contentMode: .fit)
            Rectangle()
                .foregroundColor(.blue)
                .frame(width: 100, height: 100)
                .opacity(0.8)
            Rectangle()
                .foregroundColor(.red)
                .frame(width: 200, height: 200)
                .opacity(0.8)
                .offset(x: 100, y: 100)
        }
    }
}

このスクラップは2021/01/19にクローズされました