Search
Duplicate

ViewModifier

상태
완료
A modifier that you apply to a view or another view modifier, producing a different version of the original value.
모디파이어는 뷰 또는 다른 뷰 모디파이어에 적용가능하며 원본 값의 다른 버젼을 생산한다.

Declaration

protocol ViewModifier

Overview

Adopt the ViewModifier protocol when you want to create a reusable modifier that you can apply to any view. The example below combines several modifiers to create a new modifier that you can use to create blue caption text surrounded by a rounded rectangle:
ViewModifier 프로토콜을 채택하면 사용자는 재사용가능한 모디파이어를 만들 수 있다. 이렇게 만들어진 모디파이어는 모든 뷰에 적용이 가능해진다.
아래의 예시는 몇몇의 모디파이어들을 조합하여서 새로운 모디파이어를 만들어내는 예시이다. 파란색 둥근 직사각형으로 둘러싸여진 파란색 캡션 텍스트를 만드는 모디파이어를 만들어보자.
struct BorderedCaption: ViewModifier { func body(content: Content) -> some View { content .font(.caption2) .padding(10) .overlay( RoundedRectangle(cornerRadius: 15) .stroke(lineWidth: 1) ) .foregroundColor(Color.blue) } }
Swift
복사
You can apply modifier(:) directly to a view, but a more common and idiomatic approach uses modifier(:) to define an extension to View itself that incorporates the view modifier:
modifier(_:)를 뷰에 직접적으로 적용시킬 수 있지만 보편적이고 이상적인 접근은 모디파이어를 뷰 자체의 extension 으로 정의하는 것이다.
extension View { func borderedCaption() -> some View { modifier(BorderedCaption()) } }
Swift
복사
You can then apply the bordered caption to any view, similar to this:
Image(systemName: "bus") .resizable() .frame(width:50, height:50) Text("Downtown Bus") .borderedCaption()
Swift
복사