- View Modifier2023년 12월 14일
- 2료일
- 작성자
- 2023.12.14.:42
A modifier that you apply to a view or another view modifier, producing a different version of the original value.
- 기존의 뷰 또는 다른 view modifier에 적용시켜 또 다른 버전을 만들 수 있는 modifier이다. 즉 기존의 뷰 또는 modifier에 추가적으로 꾸며줄수 있다.
그런데 여기서 궁금한점... 왜? 굳이 커스텀해서 뷰 모디파이러를 하는가 그자체로 바꾸지 않구?
- 앱 전체적으로 쓰이는 스타일이 있다면 일일이 수정해주려면 코드를 일일이 추가해주어야하기에
struct RedBorder: ViewModifier { func body(content: Content) -> some View { content .border(Color.red, width: 1) } }
Text("Hello, world!") .modifier(RedBorder())
이런식으로 한번에 텍스트를 내가 원하는 방식으로 커스텀이 가능해진다.
아니면 view의 확장을 이용할수도 있다.
extension View { func borderRed() -> some View { modifier(RedBorder()) } struct ContentView: View { var body: some View { Text("hi") .borderRed() } }
위와 같은 방법 아니면
extension View { func borederRed() -> some View { self. foregroundColor(Color.red) } } struct ContentView: View { var body: some View{ Text("Red") .borederRed() }}
이것도 아니면 새로운 뷰로 빼는 방법도 있다.
struct BorderRed<Content: View>: View { let content: Content init(@ViewBuilder content:() -> Content) { self.content = content() } var body: some View { content .overlay( RoundedRectangle(cornerRadius: 14) .stroke(lineWidth: 1) ) .foregroundStyle(Color.red) } } struct ContentView: View { var body: some View { BorderRed(content:{Text("red")})}}
어떤 방법으로 할지는 팀원과의 조율하면 된다.
1. 하나의 뷰모디파이어로 만들어서 호출
2. 메소드로 만들어서 호출
3. 하나의 struct로 만들어서 호출
하지만 결국 공통된것을 하나로 만들어서 쉽게 호출할수 있는 것!
'SWIFT개발' 카테고리의 다른 글
근본으로 돌아가자(3)-View Layout (2) 2024.02.26 근본으로 돌아가자(2) - animation & iOS 17에서 바뀐것들 (3) 2023.12.14 Spritekit with swiftui(1) (0) 2023.05.01 CoreMotion - Swift (1) 2023.04.29 Swift? 난 이걸 왜 하지? (자기소개 포함) (2) 2023.04.04 다음글이전글이전 글이 없습니다.댓글
스킨 업데이트 안내
현재 이용하고 계신 스킨의 버전보다 더 높은 최신 버전이 감지 되었습니다. 최신버전 스킨 파일을 다운로드 받을 수 있는 페이지로 이동하시겠습니까?
("아니오" 를 선택할 시 30일 동안 최신 버전이 감지되어도 모달 창이 표시되지 않습니다.)