简体   繁体   中英

Making a custom ButtonStyle in SwiftUI (for MacOS)

Mine isn't working 100% for some reason. Some modifiers are working and some aren't (noted below). My View struct is like this:

    struct MyView: View {

      // ...

          struct CustomButtonStyle: ButtonStyle {
             func makeBody(configuration: CustomButtonStyle.Configuration) -> some View {
                 configuration.label
                     .foregroundColor(Color.white)                      // this works
                     .frame(width: 330, height: 40, alignment: .center) // this doesn't work
                     .clipShape(RoundedRectangle(cornerRadius: 8))      // this doesn't work
                     .padding(100)                                      // this works
             }
         }

      // ...

      // in var body...
        Button(action: {
            // ...
        }, label: {
        Text("Confirm")
            .fontWeight(.semibold)
            .background(Color.blue)
        })
        .buttonStyle(CustomButtonStyle())

When I put the ViewModifiers on the Text view in the Button's label, it works fine, but not sure why this doesn't work. TIA!

Figured it out, but leaving up here for others. The modifiers weren't in the correct order so it some were just nonexistent. Also I tried applying the custom style to an Image button, but I needed to set the rendering mode to .template .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM