Home > Mobile >  Swift UI - I can't figure out why this button won't print the action I set
Swift UI - I can't figure out why this button won't print the action I set

Time:05-22

Ive been trying to figure out why my button won't perform the action I set it to. Any help would be appreciated!

The code works by sending in an action as input. I used another stack overflow linked in the code to get it but I cant quite figure out why it won't do the action. I tried setting another button in the view but it wouldn't do that action as well. Please help!

//
//  LargeButton.swift
//  Plan-It
//
//  Created by Aarya Chandak on 5/13/22.
// https://stackoverflow.com/questions/58928774/button-border-with-corner-radius-in-swift-ui

import SwiftUI

struct LargeButtonStyle: ButtonStyle {
    
    let backgroundColor: Color
    let foregroundColor: Color
    let isDisabled: Bool
    
    func makeBody(configuration: Self.Configuration) -> some View {
        let currentForegroundColor = isDisabled || configuration.isPressed ? foregroundColor.opacity(0.3) : foregroundColor
        return configuration.label
            .padding()
            .foregroundColor(currentForegroundColor)
            .background(isDisabled || configuration.isPressed ? backgroundColor.opacity(0.3) : backgroundColor)
            // This is the key part, we are using both an overlay as well as cornerRadius
            .cornerRadius(6)
            .overlay(
                RoundedRectangle(cornerRadius: 6)
                    .stroke(currentForegroundColor, lineWidth: 1)
        )
            .padding([.top, .bottom], 10)
            .font(Font.system(size: 19, weight: .semibold))
    }
}

struct LargeButton: View {
    
    private static let buttonHorizontalMargins: CGFloat = 20
    
    var backgroundColor: Color
    var foregroundColor: Color
    
    private let title: String
    private let action: () -> Void
    
    // It would be nice to make this into a binding.
    private let disabled: Bool
    
    init(title: String,
         disabled: Bool = false,
         backgroundColor: Color = Color.green,
         foregroundColor: Color = Color.white,
         action: @escaping () -> Void) {
        self.backgroundColor = backgroundColor
        self.foregroundColor = foregroundColor
        self.title = title
        self.action = action
        self.disabled = disabled
    }
    
    var body: some View {
        HStack {
            Spacer(minLength: LargeButton.buttonHorizontalMargins)
            Button(action:self.action) {
                Text(self.title)
                    .frame(maxWidth:.infinity)
            }
            .buttonStyle(LargeButtonStyle(backgroundColor: backgroundColor,
                                          foregroundColor: foregroundColor,
                                          isDisabled: disabled))
                .disabled(self.disabled)
            Spacer(minLength: LargeButton.buttonHorizontalMargins)
        }
        .frame(maxWidth:.infinity)
    }
}

struct LargeButton_Previews: PreviewProvider {
    static var previews: some View {
        LargeButton(title: "Invite a Friend",
                    backgroundColor: Color.white,
                    foregroundColor: Color.green) {
                                print("Hello World")
                            }
    }
}

CodePudding user response:

You've set the button action only in the preview code.

But it's a print, and prints from previews don't get channeled to the Xcode's console.

Define the action in the actual UI of your app and run it, you should see the result of the print call in the console.

  • Related