如何在WatchOS中用背景视图填充整个Button区域? [英] How do you fill entire Button area with background View in WatchOS?

查看:107
本文介绍了如何在WatchOS中用背景视图填充整个Button区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的背景视图跨越Apple Watch外形尺寸的Button的整个区域.

I am trying to have my background view span the entire area of a Button on an Apple Watch form factor.

struct SurveyView: View {

var body: some View {
    Button(action: {
        print("Test tapped.")
    }) {
        HStack {
            Text("Test")
                .fontWeight(.semibold)
        }
        .frame(minWidth: 0, maxWidth: .infinity)
        .padding()
        .foregroundColor(.white)
        .background(LinearGradient(gradient: Gradient(colors: [.green, .blue]), startPoint: .leading, endPoint: .trailing))


     }
   }
}

如何使背景覆盖整个Button?

How can I make the background span the entire Button?

更新:原来我的问题还与进入列表视图有关.设置遮罩并不能完全解决问题.

Update: Turns out my issue is also related to being in a List View. Setting a mask doesn't quite solve it.

struct SurveyView: View {

var body: some View {
    List() {
        Button(action: {
            print("Test tapped.")
        }) {
            Text("Test")
                .fontWeight(.semibold)
        }
        .background(Color.orange)


        .mask(RoundedRectangle(cornerRadius: 24))
        }
    }
}

推荐答案

我将通过以下方式进行此操作:

I would do this in the following way:

Button(action: {
    print("Test tapped.")
}) {
    Text("Test")
        .fontWeight(.semibold)
        .padding()
        .foregroundColor(.white)
}
.background(LinearGradient(gradient: Gradient(colors: [.green, .blue]), startPoint: .leading, endPoint: .trailing))
.mask(RoundedRectangle(cornerRadius: 24))

更新:列表的情况

var body: some View {
    List() {
        Button(action: {
            print("Test tapped.")
        }) {
            Text("Test")
                .fontWeight(.semibold)
                .padding()
        }
        .background(Color.orange)
        .mask(RoundedRectangle(cornerRadius: 24))
        .listRowPlatterColor(Color.clear)
    }
}

这篇关于如何在WatchOS中用背景视图填充整个Button区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆