問題如下
ZStack內的Color.clear是透明色時
該Button點擊範圍為Text的文字長度
會導致點擊的範圍,只能點在文字上
如果Button width = 200,Text width = 100, 點擊範圍只有"100″在文字上
如果點擊在文字外,將無法點擊
Button(action: { print("click") }, label: { ZStack { Color.clear Text("1234567") .foregroundColor(contentIndex == index ? Color("blue_2") : Color("gray_5")) .padding() } })
解決方法如下:
Color.clear用GeometryReader包住
取得此區域的最大Size
然後contentShape設定能點擊的範圍
設定後,此Color.clear將是Button能點擊範圍
不會因為設定成透明色而無法點擊
Button(action: { print("click") }, label: { ZStack { GeometryReader { geometry in Color.clear.contentShape(Path(CGRect(origin: .zero, size: geometry.size))) } Text(contentArrayTemp[index].id ?? "") .foregroundColor(contentIndex == index ? Color("blue_2") : Color("gray_5")) .padding() } })
訂閱Codeilin的旅程,若有最新消息會通知。
廣告
發表迴響