Protocol

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:


struct BorderedCaption: ViewModifier {
    func body(content: Content) -> some View {
        content
            .font(.caption2)
            .padding(10)
            .overlay(
                RoundedRectangle(cornerRadius: 15)
                    .stroke(lineWidth: 1)
            )
            .foregroundColor(Color.blue)
    }
}

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:


extension View {
    func borderedCaption() -> some View {
        modifier(BorderedCaption())
    }
}

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()

A screenshot showing the image of a bus with a caption reading

Topics 話題

Getting the Body ボディを取得する

Adding Animations to a View アニメーションをビューに加える

Handling View Taps and Gestures ビューのタップとジェスチャーを取り扱う

Relationships 関係

Inherited By 継承される先

Conforming Types これらの型が準拠

See Also 参照

View Modifiers ビュー修飾子