According to the documentation of ".offset", The original dimensions of the view aren’t changed by offsetting the contents; in the example below the green border drawn by this view surrounds the original position of the text:
Text("Hello, World!")
.border(Color.black)
.offset(x: 10.0, y: 50)
.border(Color.green)
IMO, the modifer (ie,.border(Color.green))followed ".offset" should only affect the original view, but not the new view.
When add a ".frame" right after the ".offset", I thought it also only affect the original view. However, the original and the new view are both affected.
Text("Hello, World!")
.border(Color.black)
.offset(x: 10.0, y: 50)
		.frame(width:50)
.border(Color.green)
Further more, if I keep adding another ".frame" right after the first one, only the original view is affected again.
Text("Hello, World!")
.border(Color.black)
.offset(x: 10.0, y: 50)
.frame(width:50)
.frame(width:100)
.border(Color.green)
What happened under the hood? Thanks in advanced.
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I want to test the code below in live preview, but it seems not to work. In simulator, the button can change the text well, but not in live preview. How to fix it? Thanks in advance.
import SwiftUI
struct PreviewOnchangeTest: View {
@Binding var toggle: Bool
@State var text = "nice"
var body: some View {
VStack{
Text("\(text)")
.onChange(of: toggle, perform: { value in
text = toggle.description
})
Button(action: {toggle.toggle()}, label: {
Text("change")
	})
}
}
}
struct PreviewOnchangeTest_Previews: PreviewProvider {
@State static var toggle = true
static var previews: some View {
PreviewOnchangeTest(toggle: $toggle)
}
}