22

I know how to add this element with HStack and Rectangle. But...

  1. How is it called?

  2. Does SwiftUI have this element out of the box?

Unknown element

1

4 Answers 4

33

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Interestingly, the drag indicator is not displayed if there is a Form on the sheet.
14
  1. The thing is called a Drag Indicator.
  2. With iOS 16+ you can use .presentationDragIndicator(_:) modifier to control its visibility.

Proposals have been made on how to emulate this drag indicator in earlier iOS versions.

As a small addition to the great answers of John M. and happymacaron, this comes even closer:

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

2 Comments

great but in dark mode it should be a bit brighter than this. I moved the opacity logic into a var that computes based on colorScheme, approx 0.7 for dark mode
@aehlke: Smart!
10

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.

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

2 Comments

When I place a Navigationview under it, it does not look like in the image tho.
How do you make the background behind the drag bar transparent when used in a .sheet modal?
4

To add to John M.'s wonderful answer, you can use a ZStack to place the Capsule on top of the 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 close to the image.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.