初始化自定义按钮 SwiftUI

Init Custom Button SwiftUI

正在尝试初始化 CustomButton(标题:“添加”,图标:.add,状态:.enable)

我的代码如下。我确实得到了标题,但枚举不起作用。

加上接收错误

无法将类型 'Image' 的值转换为预期的参数类型 'String'

在图片(图标)上


import SwiftUI

struct CustomButton: View {
       var title: String
       var icon: String
       var status: Color
    
    var body: some View {
        Button(action: {

        }) {
            Text(title)
                .foregroundColor(.white)
                .background(Color(.green))
                .font(Font.custom("SFCompactDisplay", size: 14))
                
            Image(icon)
                .renderingMode(.original)
                .foregroundColor(.white)
        }
    }
    
    enum Icon {
        case add
        case edit
        
        var image: Image {
            switch self {
            case .add:
                return Image("Add")
            case .edit:
                return Image("Edit")
            }
        }
    }
    
    enum Status {
        case enable
        case disable
        
        var color : Color {
            switch self {
            case .enable:
                return Color(.green)
            case .disable:
                return Color(.gray)
            }
        }
    }
    
    init(title: String, icon: Icon, status: Status) {
        self.title = title
        self.icon = icon.image
        self.status = status.color
    }
}
 

我猜你想要这个

struct CustomButton: View {
       var title: String
       var icon: Icon
       var status: Color

    var body: some View {
        Button(action: {

        }) {
            Text(title)
                .foregroundColor(.white)
                .background(Color(.green))
                .font(Font.custom("SFCompactDisplay", size: 14))

            icon.image
                .renderingMode(.original)
                .foregroundColor(.white)
        }
    }

    enum Icon {
        case add
        case edit

        var image: Image {
            switch self {
            case .add:
                return Image("Add")
            case .edit:
                return Image("Edit")
            }
        }
    }

    enum Status {
        case enable
        case disable

        var color : Color {
            switch self {
            case .enable:
                return Color(.green)
            case .disable:
                return Color(.gray)
            }
        }
    }

    init(title: String, icon: Icon, status: Status) {
        self.title = title
        self.icon = icon
        self.status = status.color
    }
}

我明白了。现在可以使用了。

struct CustomButton: View {
    let title: String
    let icon : String
    let status: Color
    @State private var buttonDisabled = true
    
    var body: some View {
        Button(action: {
            
        }) {
            ZStack(alignment:.bottom) {
                HStack {
                        Text(title)
                            .foregroundColor(.white)
                             .font(Font.custom("SFCompactDisplay-Bold", size: 20))
                            .bold()
                            .fontWeight(.bold)
                            .background(status)
                        Image(icon)
                            .renderingMode(.original)
                            .foregroundColor(.white)
                            .background(Color(.white))
                }
                .frame(width: 335, height: 20, alignment: .center)
                .padding()
                .background(status)
            }
        .cornerRadius(10)
           
        }
    }
    
    enum Icon {
        case add
        case edit
        case none
        
        var image: String {
            switch self {
            case .add:
                return "Add"
            case .edit:
                return "Edit"
            case .none:
                return "empty"
            }
        }
    }

    enum Status {
        case enable
        case disable
    }
    
    init(title: String, icon: Icon, status: Status) {
        self.title = title
        self.icon = icon.image
        if status == .enable {
            self.status = Color(#colorLiteral(red: 0, green: 0.6588235294, blue: 0.5254901961, alpha: 1))
        } else {
            self.status = Color(#colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1))
        }
        
    }
}

struct CustomButton_Previews: PreviewProvider {
    static var previews: some View {
        CustomButton(title: "Odeme Yontemi Ekle", icon: .none, status: .enable)
    }
}