Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inherit from a SwiftUI Button

Tags:

swift

swiftui

Is it possible to inherit from a Button in SwiftUI? I tired it and I get an error every time, since Button is expecting some kind of generic argument.

import SwiftUI

struct ButtonSubClass: Button<Label: View> {
    var body: some View {
        Text("Hello, World!")
    }
}

XCode automatically adds the <Label: View> part, but I don't know what it means or what I actually need to put in the braces. I tried Button<View>, but then I get the error Protocol type 'View' cannot conform to 'View' because only concrete types can conform to protocols

like image 330
Sheldon Avatar asked Jan 25 '23 14:01

Sheldon


1 Answers

Button is a struct, which cannot be subclassed. If you are trying to create a custom View with Button-like properties, but customized in some way, you can apply view modifiers or use the ButtonStyle protocol.

If you can supply some specifics of what you are trying to accomplish, I will try to add some example code.

like image 128
John M. Avatar answered Jan 29 '23 21:01

John M.