SWIFT6 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. keyPath의 함수기능 User 구조체를 정의하고 그 배열을 만들자 struct User { let email: String let isAdmin: Bool } let users = [User(email: "A", isAdmin: true), User(email: "B", isAdmin: false), User(email: "C", isAdmin: true), User(email: "D", isAdmin: false)] 그리고 map을 이용해서 email이나 isAdmin값을 가져오거나 filter를 사용하려면 클로저를 전달해야한다. users.map{$0.email} users.map{$0.isAdmin} users.filter{$0.isAdmin} 하지만 keyPath를 함수처럼 사용해서 더 예쁘게 작성할 수 있다. use.. 2023. 8. 31. UIImageView를 Image 크기에 맞추기 [Swift] 문제점: UIImageView에서 scaleToFit 설정을 준 후, constraint를 통해 width가 화면 뷰에 맞도록 고정시켰다. 하지만 UIImagView의 크기가 줄어들지 않아 남는 여백이 생겨 UIImageView를 Image 크기에 맞게 AutoLayout을 설정하려 한다. 해결법: 1. Image의 비율을 저장한다. 2. AutoLayout의 height 설정에서 저장한 비율을 이용해 UIImageView의 width값으로 height값을 구해 AutoLayout을 설정한다. private let postImageView: UIImageView = { let iv = UIImageView() iv.contentMode = .scaleAspectFit iv.layer.borderWidth.. 2023. 8. 26. 이전 1 2 다음