简体   繁体   English

SwiftUI 如何在sheet上添加灰线条

[英]SwiftUI how to add gray line bar on sheet

I know how to add this element with HStack and rectangle.我知道如何使用 HStack 和矩形添加此元素。 But...但...

1) How is it called? 1)它怎么称呼?

2) Does SwiftUI has this element in the box? 2) SwiftUI 盒子里有这个元素吗?

未知元素

I don't think there's one accepted name for it;我认为它没有一个公认的名称。 I usually call it a drag handle as it's there to indicate a draggable area to users.我通常称它为拖动手柄,因为它在那里向用户指示可拖动区域。

There is no built-in syntax to add one to a sheet.没有将一个添加到工作表的内置语法。 That's the benefit of SwiftUI – simple controls are easy to implement and customize.这就是 SwiftUI 的优势——简单的控制易于实施和定制。

For completeness, you can make one using something like the following:为了完整起见,您可以使用以下内容制作一个:

VStack {
    Capsule()
        .fill(Color.secondary)
        .frame(width: 30, height: 3)
        .padding(10)

    // your sheet content here

    Spacer()
}

iOS16: iOS16:

It is added automatically if you have more than one detent, so for just one detent you will have to be explicit about the visibility:如果您有多个定位器,它会自动添加,因此对于一个定位器,您必须明确说明可见性:

yourContentView
    .presentationDetents([.medium])
    .presentationDragIndicator(.visible)

To add to John M.'s wonderful answer, you can use a ZStack to place the Capsule on top of the NavigationView .要添加到 John M. 的精彩答案,您可以使用ZStack将 Capsule 放在NavigationView的顶部。 Like this:像这样:

ZStack(alignment: .top) {
    // NavigationView
    Capsule()
        .fill(Color.secondary)
        .frame(width: 35, height: 5)
}

I experimented with the size and found width: 35, height: 5 produces a shape that is closed to the image.我尝试了大小,发现width: 35, height: 5会产生一个接近图像的形状。

As a small addition to the great answers of John M. and happymacaron, this comes even closer:作为对 John M. 和 happymacaron 的精彩答案的一个小补充,这更接近:

ZStack(alignment: .top) {
    // NavigationView
    Capsule()
        .fill(Color.secondary)
        .opacity(0.5)
        .frame(width: 35, height: 5)
        .padding(6)
}

Update for IOS 16 / Xcode 14更新 IOS 16 / Xcode 14

Expanding on these great answers.扩展这些伟大的答案。 I have several sheets that can be presented by an ActiveSheet enum and wanted to embed a top/drag bar once for all sheets.我有几张可以由 ActiveSheet 枚举呈现的工作表,并且想为所有工作表嵌入一次顶部/拖动条。 I'm also using IOS16's new presentationDetents我也在使用 IOS16 的新 presentationDetents

   @ViewBuilder
    private func sheetView(with item: ActiveSheet) -> some View {
        VStack {
            Capsule()
                .fill(Color.secondary)
                .frame(width: 45, height: 5)
                .padding()
            
            switch item {
            case .addItem:
                AddItemView(activeSheet: $activeSheet, list: $selectedList) // bind a list here
            case .addList:
                AddListView(activeSheet: $activeSheet)
            case .showParticipants (let participants, let list):
                ParticipantView(listOfParticipants: participants, list: list)
            default:
                EmptyView()
            }
            Spacer()
        }.presentationDetents([.medium, .large])
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM