Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
ToolbarContent: add a .disabled(Bool) method

I've created a custom ToolbarContent for common buttons, such as Save, Edit, Cancel, etc...

For example:


import SwiftUI

public struct SaveToolbarItem: ToolbarContent {
    private let placement: ToolbarItemPlacement
    private let action: () -> Void

    public init(placement: ToolbarItemPlacement = .confirmationAction,
                action: @escaping () -> Void = {}) {
        self.placement = placement
        self.action = action
    }

    public var body: some ToolbarContent {
        ToolbarItem(placement: placement) {
            Button(
                "Save"
            ) {
                action()
            }
        }
    }
}

At call site:

        .toolbar {            
            SaveToolbarItem() {
                 // some code for the Action
                presentationMode.wrappedValue.dismiss()
            }
            .disabled(true) // Value of type 'SaveToolbarItem' has no member 'disabled'
        }

However, I'm getting the following error while trying to use isDisabled method:

Value of type 'SaveToolbarItem' has no member 'disabled'

How can I mitigate this error? E.g. maybe define disabled only on my custom types of ToolbarContent?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to place it inside, like

public var body: some ToolbarContent {
    ToolbarItem(placement: placement) {
        Button(
            "Save"
        ) {
            action()
        }
        .disabled(true)     // << here !!
    }
}

*of course parameter for disabled is expected to be passed in.

Updated: Here is possible approach with built-in disabled modifier.

Tested with Xcode 13.2 / iOS 15.2

demo

public struct SaveToolbarItem: ToolbarContent {
    private let placement: ToolbarItemPlacement
    private let action: () -> Void

    private var enabled = true

    public init(placement: ToolbarItemPlacement = .confirmationAction,
                action: @escaping () -> Void = {}) {
        self.placement = placement
        self.action = action
    }

    public var body: some ToolbarContent {
        ToolbarItem(placement: placement) {
            Button(
                "Save"
            ) {
                action()
            }
            .disabled(!enabled)
        }
    }

    func disabled(_ flag: Bool) -> Self {
        var newItem = self
        newItem.enabled = !flag
        return newItem
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!