【问题标题】:Init Custom Button SwiftUI初始化自定义按钮 SwiftUI
【发布时间】:2020-12-30 00:53:51
【问题描述】:

尝试初始化 CustomButton(title: "Add", icon: .add, status: .enable)

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

加上接收错误

无法将“图像”类型的值转换为预期的参数类型“字符串”

在图像(图标)


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
    }
}
 

【问题讨论】:

    标签: button enums swiftui init


    【解决方案1】:

    我猜你想要这个

    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
        }
    }
    

    【讨论】:

    • “var status: Color”替换为“var status: Status”
    • 用 Xcode 11.7 测试 - 工作正常,我只使用了 Image(systemName: "plus"),因为我没有你的资源
    • systemname 可能有效,但我需要我的 Icon 枚举才能与 init 一起使用
    • 我用它代替了你的Image("Add") 而不是枚举。也许您的资产中没有名为“添加”的图片?
    【解决方案2】:

    我想通了。现在可以使用了。

    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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-16
      • 2021-07-04
      • 1970-01-01
      • 2011-11-19
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多