How do you fill entire Button area with background View in WatchOS?

泪湿孤枕 提交于 2020-01-24 20:49:08

问题


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))


     }
   }
}

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))
        }
    }
}


回答1:


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))

Update: the case for list

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)
    }
}


来源:https://stackoverflow.com/questions/59402453/how-do-you-fill-entire-button-area-with-background-view-in-watchos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!