skip to Main Content

I’m trying to create new iOS 17 interactive widget. To be able to add "action" I want to use new init(_:intent:) initializer of Button which allow me to perform given intent when button gets pressed.

The problem is, my button is supposed to be just the green inner rectangle with the smile image, but system adds its own background with padding. Is there any way to remove it?

Example code:

Button(intent: MyIntent()) {
    MyLabel()
}

How it looks like:

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    I figured out that I have to set button's style to PlainButtonStyle. Now the background and the padding is removed.

    Button(intent: MyIntent()) {
        MyLabel()
    }.buttonStyle(PlainButtonStyle())
    

    enter image description here


  2. If you want to apply a custom style or perform a custom animation when the button is pressed you can do something like this:

    private struct MoveUpButtonStyle: ButtonStyle {
        var resizeScale: CGFloat
        var offset: CGPoint
    
        func makeBody(configuration: Configuration) -> some View {
            configuration.label
                .contentShape(Rectangle())
                .scaleEffect(configuration.isPressed ? resizeScale : 1.0)
                .offset(x: configuration.isPressed ? offset.x : 0, y: configuration.isPressed ? offset.y : 0)
        }
    }
    

    And call it like so:

    .buttonStyle(MoveUpButtonStyle(resizeScale: 2.0, offset: CGPoint(x: 0, y: -40)))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search