SwiftUI-Button点击区域问题

1,027 阅读1分钟

源代码:这句代码这样我发现只有点击中间文字”Login”区域才会触发点击方法

Button(”Login”) {
	autheticateUser(username: username, password: password)
}
 .foregroundColor(.white)
 .frame(width: 300,height: 50)
 .background(Color.blue)
 .cornerRadius(10)
 

💡 原因: button实际上是Button默认点击范围是其内容Label的范围,如果比button的frame小,点击边缘区域将无法触发

解决:IOS16更改了button写法如下:

Button() {

} label: {
	Text ("Login")
}
                    
 .foregroundColor(.white)
 .frame(width: 300,height: 50)
  .background(Color.blue)
  .cornerRadius(10)
  .onTapGesture {
                        	
   }
}