SwiftUI onTapGesture 生效区域

513 阅读1分钟
VStack {

    Text("暂无搜索结果")

        .foregroundColor(AppColor.lightGrey)

}

.frame(maxWidth: .infinity, maxHeight: .infinity)

.border(.red)

.background(Color.white.onTapGesture {

    // 事件...
    // 点击背景,键盘退下

})

onTapGesture 事件生效区域取决于VStack子视图的大小范围,而我们需要整个父视图范围的点击响应,就需要将onTapGesture加在背景上,如下:

VStack {

    Text("暂无搜索结果")

        .foregroundColor(AppColor.lightGrey)

}

.frame(maxWidth: .infinity, maxHeight: .infinity)

.border(.red)

.onTapGesture {

    // 事件...
    // 点击背景,键盘退下

}