Кнопка не может быть выбрана в tvOS с использованием собственного ButtonStyle в SwiftUI - PullRequest
1 голос
/ 01 октября 2019

После замены стандартного стиля кнопки на пользовательский кнопка больше не может быть выбрана в tvOS (она работает должным образом в iOS). Есть ли в PlainButtonStyle () специальный модификатор, который мне не хватает? Или это ошибка в SwiftUI?

Вот фрагмент кода, который работает:

Button(
    action: { },
    label: { Text("Start") }
).buttonStyle(PlainButtonStyle())

, а вот тот, который не работает:

Button(
    action: { },
    label: { Text("Start") }
).buttonStyle(RoundedButtonStyle())

где RoundedButtonStyle () определяется как:

struct RoundedButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .padding(6)
            .foregroundColor(Color.white)
            .background(Color.blue)
            .cornerRadius(100)
    }
}

1 Ответ

0 голосов
/ 01 октября 2019

Если вы создаете свой собственный стиль, вы должны обращаться с фокусом вручную. Конечно, есть разные способы, как вы могли бы сделать это.

struct RoundedButtonStyle: ButtonStyle {

    let focused: Bool
    func makeBody(configuration: Configuration) -> some View {
        configuration
            .label
            .padding(6)
            .foregroundColor(Color.white)
            .background(Color.blue)
            .cornerRadius(100)
            .shadow(color: .black, radius: self.focused ? 20 : 0, x: 0, y: 0) //  0

    }
}
struct ContentView: View {

    @State private var buttonFocus: Bool = false
    var body: some View {
        VStack {
            Text("Hello World")

            Button(
                action: { },
                label: { Text("Start") }
            ).buttonStyle(RoundedButtonStyle(focused: buttonFocus))
                .focusable(true) { (value) in
                    self.buttonFocus = value
            }
        }
    }
}
...