본문 바로가기

SwiftUI3

하위뷰에서 상위뷰로 데이터 전달 (PreferenceKey, GeometryReader) [SwiftUI] 문제상황 struct BlueRectangle: View { @State var blueRectSize = CGSize(width: 20, height: 40) var body: some View { VStack { Rectangle() .frame(width: blueRectSize.width, height: blueRectSize.height) .foregroundStyle(.blue) BlackRectangle() .frame(height: 60) } } } struct BlackRectangle: View { var body: some View { HStack { Rectangle() Rectangle() Rectangle() Rectangle() } } } BlueRectangle 1개와 Bla.. 2023. 9. 27.
withAnimation 버그 해결 https://stackoverflow.com/questions/77022165/why-occur-animation-effect-in-swiftui Why occur animation effect in SwiftUI struct ContentView: View { var randColor: Color { Color(red: randomNumber/255, green: randomNumber/255, blue: randomNumber/255) } @State var isAnimation = false var stackoverflow.com stackoverflow에 질문을 올렸었는데, 분명히 아래의 Rectangle은 색이 변하는 애니메이션이 작동할 이유가 없다. 하지만 지속적으로 색이 변하는 애니메이션이.. 2023. 9. 1.
ForEach의 ID [SwiftUI] ForEach View를 사용할 때 ID로 \.self를 넣어주고는 한다. struct ContentView: View { let colors: [Color] = [.red, .green, .blue] var body: some View { VStack { ForEach(colors, id: \.self) { color in Text(color.description.capitalized) .padding() .background(color) } } } } ForEach로 생성되는 각 뷰들이 생성되거나 삭제될 때 그 부분만 업데이트하기위해 ID값을 통해서 뷰를 구별한다. 이를 위해서 id인자를 넣어주게 된다. public init(_ data: Data, id: KeyPath, @ViewBuilder co.. 2023. 8. 31.