-
[SwiftUI] Button 을 Toggle 처럼 사용해보자Swift/SwiftUI 2022. 4. 5. 23:22
구현해볼 Helper는 title: Turtle Rock 옆에 있는 별 모양이다
기능적으로는 Favorite place를 mark하는 기능이다
버튼을 누르면 true : star.fill 이라고 작동한다
우리가 인스타그램에서 좋아요 누르는거랑 똑같다
근데 이건 true or false를 반환하는 toggle이 아니다
button이다 하지만 toggle처럼 작동한다 어떻게 된걸까?
Detail View 의 code 이다
FavoriteButton이라고 따로 만든 View로 전달 되고 있으며
보여주고자하는 data의 isFavorite값을 parameter로 전달하고있다
그러면 FavoriteButton View를 살펴보자
Button이지만 isSet이라는 Bool type 변수를 만들어서 .toggle() 이라는
내장함수를 통해 눌릴때 마다 true or false를 계속 바꿔준다
label: {} 부분은 이 button의 appearance에 대한 부분이다
삼항연산자를 통해 isSet true라면 systemImage : "star.fill" if not "star"로 보내준다
그러면 button의 모양이 star로 변하게 된다
그 밑에는 label에 내장함수들인데 iconOnly로 "Toggle Favorite"를 감춰버렸다
foregroundColor은 별의 색이다
'Swift > SwiftUI' 카테고리의 다른 글
[SwiftUI] Swipeable TabView 만들기 (0) 2022.04.07 [SwiftUI] TextEditor background color 바꾸기 (0) 2022.04.07 [Swift UI] ObservableObject, @Published : Data의 변화를 감시하고 알려주자 (0) 2022.04.05 [SwiftUI] isFavorite: Bool 활용하여 filter 해주기 (0) 2022.04.05 [SwiftUI] projectedValue : State으로 선언된 변수를 자식 View에서 갖다 써보자 (0) 2022.04.04