首页 > 解决方案 > SwiftUI tvOS 按钮看起来很奇怪

问题描述

我正在尝试为 iOS 和 tvOS 设置相同的 SwiftUI 按钮样式,以便两个平台都可以重用它……对于 iOS,样式按预期进行,没有任何问题,但是在 tvOS 上运行相同按钮时,它们看起来与iOS版本:

IOS:

在此处输入图像描述

电视操作系统:

在此处输入图像描述

为什么 tvOS 版本的按钮上有前导和尾随?

...
var body: some View {
    Button(action: tapEvent) {
        HStack {
            if let image = icon, let uiimage = UIImage(named: image) {
                Image(uiImage: uiimage)
                    .resizable()
                    .renderingMode(.template)
                    .foregroundColor(iconColorForState(active: active, buttonMode: mode))
                    .frame(width: 20, height: 20, alignment: .center)
            }
            if let title = label {
                Text(title)
                    .lineLimit(1)
            }
        }
    }
    .foregroundColor(foregroundColorForState(active: active, buttonMode: mode))
    .padding()
    .disabled(disabled)
    .background(self.focused ? focussedBackgroundColorFor(active: active, mode: mode) : backgroundColorFor(active: active, mode: mode))
    .frame(height: heightForButtonSize(size: size ?? .Base))
    .cornerRadius(8)
    .modifier(FocusableModifier { focused in
        self.focused = focused
    })
}

标签: iosswiftswiftuitvos

解决方案


推荐阅读