Issue with Buttons in SwiftUI on MacOS
Asked Answered
O

7

27

I'm using SwiftUI on MACOS

If I do this:

      Button(action: { } ) {
        Text("Press")
        .padding()
        .background(Color.blue)
      }

I get this:

enter image description here

and the two grey areas are the ends of a tappable button. but I would expect the button to be the shape of the blue area. Any ideas how I can get the whole blue area to be tappable.

(I did look at using .onTapGesture but this doesn't animate the button so that you know you've tapped it.)

Oneeyed answered 16/10, 2019 at 17:59 Comment(2)
This is the common issue with swiftUI. I have. faced same issue with Image.Mannuela
For someone who just wants a default button, try this: https://mcmap.net/q/473165/-swiftui-on-mac-how-do-i-designate-a-button-as-being-the-primaryAdjourn
A
28

You can achieve the look you want by using a ButtonStyle and then specifying colors and other style attributes based on the configuration values being passed in.

It would be nice if there was a happy medium where you could inherit the default button radius, automatic width based on the text length and other attributes, but at least there is the ability to specify all the attributes and get the look you want.

Hope this helps!

import SwiftUI

struct BlueButtonStyle: ButtonStyle {
    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
            .foregroundColor(configuration.isPressed ? Color.blue : Color.white)
            .background(configuration.isPressed ? Color.white : Color.blue)
            .cornerRadius(6.0)
            .padding()
    }
}

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello World")
                .frame(maxWidth: .infinity, maxHeight: .infinity)

            Button(action: {
            }) {
                Text("Press")
                    .frame(maxWidth: 100, maxHeight: 24)
            }
            .buttonStyle(BlueButtonStyle())
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Arissa answered 16/10, 2019 at 23:34 Comment(6)
Thanks @Gene Z. Ragan - works a treat :-) Will be interested to see whether Apple respond to my feedback.Oneeyed
Please post a comment if they get back to you. Glad this is working for you!Arissa
Will do. From your great base, I've taken this a bit further to make it a bit cleaner and more flexible. I will post as a separate answer.Oneeyed
Same approach also discussed at: alejandromp.com/blog/2019/06/22/swiftui-reusable-button-styleOneeyed
Thanks all! this is so buried in the minds of the experts ... is there any documentation or forum on limitations of SwiftUI on MacOS since most of the tutorials are of iOS and I wasted time tracking this down to a macOS issue.Isomorphism
@mobibob, I still think this is a bug really. See my dialogue with Apple below. I don’t know of any forum or docs 🙁Oneeyed
O
15

Inspired by @Gene Z. Ragan 's great answer I've started with that answer and taken this a bit further:

Making the ButtonStyle a bit more flexible:

struct NiceButtonStyle: ButtonStyle {
  var foregroundColor: Color
  var backgroundColor: Color
  var pressedColor: Color

  func makeBody(configuration: Self.Configuration) -> some View {
    configuration.label
      .font(.headline)
      .padding(10)
      .foregroundColor(foregroundColor)
      .background(configuration.isPressed ? pressedColor : backgroundColor)
      .cornerRadius(5)
  }
}

and then some sugar to make it cleaner at the call site:

extension View {
  func niceButton(
    foregroundColor: Color = .white,
    backgroundColor: Color = .gray,
    pressedColor: Color = .accentColor
  ) -> some View {
    self.buttonStyle(
      NiceButtonStyle(
        foregroundColor: foregroundColor,
        backgroundColor: backgroundColor,
        pressedColor: pressedColor
      )
    )
  }
}

then means we can use default colouring: white foreground, grey background and accentedColor pressedColor

  Button(action: { } ) {
    Text("Button A")
  }
  .niceButton()

or we can customise the colours:

  Button(action: { } ) {
    Text("Button B has a long description")
  }
  .niceButton(
    foregroundColor: .blue,
    backgroundColor: .yellow,
    pressedColor: .orange
  )

And we get:

enter image description here

Thanks again Gene.

Oneeyed answered 17/10, 2019 at 1:23 Comment(1)
Same approach also discussed at: alejandromp.com/blog/2019/06/22/swiftui-reusable-button-styleOneeyed
O
5

I raised this with Apple on the Feedback assistant to see if they had any useful thoughts.

Dialogue here:

I said:

If on MACOS with SwiftUI I do this:

  Button(action: { } ) {
    Text("Press")
    .padding()
    .background(Color.blue)
  }

I get a blue box with two grey bits sticking out the sides. Image attached. These are the two grey areas are the ends of a tappable button. but I would expect the button to be the shape of the blue area.

The same code works fine as expected on iOS.

(I did look at using .onTapGesture but this doesn't animate the button so that you know you've tapped it.)

Apple said:

iOS and macOS have different default ButtonStyles — iOS is borderless, macOS has that standard bezel effect.

It sounds like you’re trying to create a custom ButtonStyle (and so not have any system provided chrome). So you’ll want to create that, which can apply that blue background to the label of the button, and then apply that to your simple button with Text, e.g.

Button("Press"), action {}).buttonStyle(BluePaddingButtonStyle()

This will ensure that it has the same appearance on every platform you run it on.

I said:

Hi, Thanks for the explanation. I get what you are saying and I’m ok with the method I’ve come up with.

It still doesn’t seem right that:

  Button(action: { } ) {
    Text("Press")
    .padding()
    .background(Color.blue)
  }

should produce something so odd looking. I don’t understand why that bit of code couldn’t just produce a blue button.

As I say - it’s not a problem because I’ve worked around it but it currently doesn’t seem intuitive.

Apple said:

The provided content inside the ViewBuilder is used as the label of the button: not the entire button. The button will still come with the surrounding background, bezel, foreground styling, etc as described by it’s ButtonStyle. So if your button needs to have a very specific appearance, then it needs to customize that style: either to the BorderlessButtonStyle (though note that still does come with a specific foreground appearance style), or to a custom ButtonStyle.

My thoughts:

This did help me understand why it shows as it does but intuitively it still seems wrong !!!

Oneeyed answered 3/11, 2019 at 12:58 Comment(1)
this is useful insight. thanks for sharing.Isomorphism
B
5

You can "force" an iOS-like behavior on macOS by adding .buttonStyle(.borderless).

Button(action: { } ) {
   Text("Press")
      .padding()
      .background(Color.blue)
}
.buttonStyle(.borderless)
Brigettebrigg answered 18/11, 2021 at 23:37 Comment(0)
H
1

I don't think this is possible but you could try using this

Supports SPM, is build for Swift 5.1 and is lean

Hall answered 16/10, 2019 at 19:18 Comment(6)
Thanks @Hall - much appreciated. CustomButton looks good but I can't see how I would turn it into a View to use with SwiftUI?Oneeyed
I'm also thinking that my bit o code in my question seems reasonable - would you agree this is effectively a bug?Oneeyed
To be honest I only have used SwiftUI on iOS and just tried your code on a new project to find out if it helps to apply the modifiers to the button itself... and I played around a little bit and thought this is not the correct behaviour. I would't say it's a bug because I don't know te expected behaviour, but for me it seems wrong at leastHall
I also had been using Button in iOS and it worked fine. I was surprised that it didn't have the same or at least similar result on MacOS as part of the idea of SwiftUI is that you can move between platforms with the same code.Oneeyed
Do you know how I would get CustomButton to work with SwiftUI. I presume I need some sort of wrapper which conformed to View?Oneeyed
I've sent as "Feedback" through Apple's Feedback Assistant. Interesting to see what they say...Oneeyed
H
1

In Swift 5, this could be achieved with below simple code -

        Button("First Button") {
            print("Hello World")
        }
        .padding(10)
        .accentColor(.yellow)
        .background(Color.blue)
        .cornerRadius(10)

Screenshot of the preview

Helpmeet answered 7/1, 2023 at 15:14 Comment(0)
G
1

Disregarding almost 4 years since this issue on MacOS was found, it still exists in July 2023. The Button on MacOS simply does not support LABEL at all. Any view inside the "label" is ignored and the button has a minimal height of ~16px. That is why

    Text("Press")
        .padding()
        .background(Color.blue)

is overlaying "grey background" of "button".

My advice, do not waste your time - just use "label" as a "button" with a gesture catcher.

    Text("Press")
        .padding()
        .background(Color.blue)
        .onTapGesture {
             // reaction here
        }

This works on MacOS.

Glaucoma answered 28/7, 2023 at 3:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.