.position()
absolute position in its parentโs coordinate space.
will change frame and become a "push-out" view.
.offset()
relative to its original position.
import SwiftUI
struct PositionView: View {
let text = Text("Hello SwiftUI")
var body: some View {
HStack {
Group {
// 1. original text
text
// โญ๏ธ 2. offset: frame unchanged
// (โญ๏ธ so is background)
text.offset(x: 0, y: 50)
// โญ๏ธ 3. if you want to move background too,
// have to apply .background() first.
text
.background(.indigo)
.offset(x: 0, y: 50)
// โญ๏ธ 4. position: frame changedโ๏ธ
text.position(x: 0, y: 50)
}
.padding(.horizontal, 6)
.background(.pink)
.frame(width: 150, height: 150)
.border(.white(0.4))
}
}
}