13

Is there any way to have no title section in an Action Sheet? The Action Sheet definition in the SwiftUI code is as follows:

public struct ActionSheet {
   /// Creates an action sheet with the provided buttons.
   public init(title: Text, message: Text? = nil, buttons: [ActionSheet.Button] = [.cancel()])
   /// A button representing an operation of an action sheet presentation.
   public typealias Button = Alert.Button
}

Since title only conforms to Text, I thought perhaps adding just Text("") would do the job; however, this instead keeps the space for the title empty, rather than removing it. Giving nil instead of Text("") also doesn't work.

Also, is there any way to give an Action Sheet's button a view, like with the following:

struct ActionItem: View {

  var body: some View {

    HStack {

        Image(systemName: "phone")

        Spacer()

        Text("Call 1-408-123-4567")
    }
  }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Warrior
  • 39,156
  • 44
  • 139
  • 214
  • Well, ActionSheet is for purpose. If you need sheet with custom content, why just not use View.sheet? – Asperi Nov 06 '19 at 05:36
  • 1
    @Asperi if you look at iPhones own Contacts app does it without a Title. Try and delete a contact and the actionsheet has no title and just two buttons – Learn2Code Sep 06 '20 at 16:01

2 Answers2

16

The short answer is no. SwiftUI's current implementation will always make room for a title, and will only take Text views for its buttons.

It is unclear yet whether this is because Apple only had so much time before releasing SwiftUI this year, and wanted to tackle the simplest and most common use case, or whether they are taking a principled stance that ActionSheets should always have a standard look, including title and only text buttons. We'll have to wait and see.

John M.
  • 8,892
  • 4
  • 31
  • 42
  • 4
    There is a valid reason for not showing titles. For example, the action sheet shown for a list in the Reminders app when tapping the ellipsis button has various actions and no title. IMHO it would be redundant to show a title here like "Select an Action". – bcause Jun 04 '20 at 21:33
  • 2
    Apple does this for its Contacts app. Try and delete a contact in your contacts list and the action sheet is displayed to confirm the deletion with only two buttons and no title. – Learn2Code Sep 06 '20 at 16:02
  • For anybody reading this, please file a feedback ( https://feedbackassistant.apple.com ) and feel free to mention mine FB7721684 – Federico Zanetello Dec 14 '20 at 04:13
7

Apple has answered our calls with iOS 15 and the newly introduced confirmationDialog API. ActionSheet has also been deprecated in this release.

ConfirmationDialog has been added as a view modifier and can be used on any view, very similar to the .actionSheet API we were using prior. When using the new dialog we can specify that the title is hidden and can use the Button API for role to control button appearance.

Below is an illustration of using it on a simple View.

struct ContentView: View {
    
    @State private var isShowingDialog: Bool = false
    
    var body: some View {
        VStack {
            Button {
                isShowingDialog = true
            } label: {
                Text("Tap Me")
            }
             .confirmationDialog("", isPresented: $isShowingDialog, titleVisibility: .hidden) {
                 Button("Normal") {
                 }
                 Button("Delete", role: .destructive) {
                 }
                 Button("Cancel", role: .cancel) {
                 }
            }
        }
    }
}

enter image description here

Daniel Galasko
  • 23,617
  • 8
  • 77
  • 97