Instance Method インスタンスメソッド

hidden()

Hides this view unconditionally.

Declaration 宣言

func hidden() -> some View

Return Value 戻り値

A hidden view. 隠されたビュー。

Discussion 議論

Hidden views are invisible and can’t receive or respond to interactions. 隠されたビューは不可視です、そして相互作用を受け取ったり応答したり出来ません。 However, they do remain in the view hierarchy and affect layout. Use this modifier if you want to include a view for layout purposes, but don’t want it to display.


HStack {
    Image(systemName: "a.circle.fill")
    Image(systemName: "b.circle.fill")
    Image(systemName: "c.circle.fill")
        .hidden()
    Image(systemName: "d.circle.fill")
}

The third circle takes up space, because it’s still present, but SwiftUI doesn’t draw it onscreen.

A row of circles with the letters A, B, and D, with a gap where

If you want to conditionally include a view in the view hierarchy, use an if statement instead:


VStack {
    HStack {
        Image(systemName: "a.circle.fill")
        Image(systemName: "b.circle.fill")
        if !isHidden {
            Image(systemName: "c.circle.fill")
        }
        Image(systemName: "d.circle.fill")
    }
    Toggle("Hide", isOn: $isHidden)
}

Depending on the current value of the isHidden state variable in the example above, controlled by the Toggle instance, SwiftUI draws the circle or completely omits it from the layout.

Two side by side groups of items, each composed of a toggle beneath