17.4 C
London
Wednesday, September 4, 2024

ios – SwiftUI Preview Buttons in pressed state


I’am attempting to preview buttons in pressed state however do not know the right way to obtain this.
I do not wish to retailer a variable solely used for preview within the view or within the mannequin. I can not discover a technique to simulate configuration.ispressed true for the preview, possibly it is unimaginable ?
Following is my precise code:

The View:

import SwiftUI

public struct TextButtonView: View {
    @ObservedObject var mannequin: TextButtonModel
    
    public init(mannequin: TextButtonModel) {
        self.mannequin = mannequin
    }
    
    public var physique: some View {
        Button(motion: {
            
        }) {
            TextView(mannequin: mannequin.textModel)
        }
        .buttonStyle(ButtonViewStyle(mannequin: mannequin))
    }
}

The Type:

import Basis

import SwiftUI

struct ButtonViewStyle: ButtonStyle {
    let mannequin: ButtonModel

    @ViewBuilder
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .padding(EdgeInsets(uiEdgeInsets: mannequin.getPadding()))
            .foregroundColor(.purple)
            .background(mannequin.getBackgroundColor(isPressed: configuration.isPressed))
            .cornerRadius(999)
    }
}

And my preview:

#Preview {
    return VStack {
        HStack {
            TextButtonView(mannequin: TextButtonModel(textModel: TextModel(textual content: "Physique", colour: Colour.White, fontSize: FontSize.Physique, fontWeight: .Semibold), colour: Colour.Major, sort: ButtonType.Stuffed, measurement: ButtonSize.Regular))
            TextButtonView(mannequin: TextButtonModel(textModel: TextModel(textual content: "Subheadline", colour: Colour.White, fontSize: FontSize.Physique, fontWeight: .Semibold), colour: Colour.Major, sort: ButtonType.Stuffed, measurement: ButtonSize.Small))
            TextButtonView(mannequin: TextButtonModel(textModel: TextModel(textual content: "Physique", colour: Colour.White,fontSize: FontSize.Physique, fontWeight: .Semibold), colour: Colour.Major, sort: ButtonType.Stuffed, measurement: ButtonSize.Massive))
        }
    }
}

Thanks !

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here