按钮下方的Swiftui Tap区域
我的应用中我有一个view
,该应用显示image
是背景(通过banding
),然后它具有一个按钮来下载图像和另一个是最喜欢的图像。
问题在于,当我在整个zstack
上使用.ignoresafearea
时,按钮上的点击区域移至按钮正文下方。我将图像缩放为适合和剪裁,然后尝试用填充方式将按钮向下移动,并且我尝试查看是否可以替代使整个ZSTACK
忽略没有运气的安全区域。
我还应该提到,我将图像作为背景.background zstack
的顶部,但这也会影响按钮位置。
我还应该提到我对Swift是相对陌生的
struct photoDetail: View {
let image: realPhoto
@State var textControl: Bool
init(image: realPhoto) {
self.image = image
self.textControl = image.favorited
}
var body: some View {
ZStack {
VStack(alignment: .leading) {
Spacer()
HStack {
Text("created by: \(image.username)")
.padding(5)
.foregroundColor(.white)
.background(Color(.gray).opacity(0.8))
.padding(.leading, 20)
.font(.system(size:20, weight: .semibold))
.padding(.bottom, 50)
.cornerRadius(4)
Spacer()
}.padding(.bottom, 60)
}
VStack {
HStack {
Spacer()
Button {
let imageSaver = ImageSaver()
imageSaver.writeToPhotoAlbum(image: UIImage(data: image.image)!)
} label: {
Image(systemName: "square.and.arrow.down")
.resizable()
.scaledToFit()
.frame(width: 20)
.padding(8)
}
Button {
favoriteToggle()
} label: {
Image(systemName: textControl == false || image.favorited == false ? "heart": "heart.fill")
.resizable()
.scaledToFit()
.frame(width: 25)
.padding(8)
}.padding(.trailing, 30)
}.padding(.top, 100)
Spacer()
}
}.onAppear() {
self.textControl = image.favorited
}.background(
Image(uiImage: UIImage(data: image.image)!)
.resizable()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.scaledToFill()
.clipped()
).ignoresSafeArea()
}
}
I have a View
in my app that shows an Image
as the background (via Binding
) and then it has a button to download the image and another to favorite the image.
The problem is that when I use the .ignoreSafeArea
on a whole ZStack
, the tap area on the button shifts to below the button body. I have the images scaled to fit and clipped and I tried moving the buttons down with padding, and I have tried seeing if there was an alternative to making the whole ZStack
ignore the safe area with no luck.
I should also mention that I have put the image as the background with the .background
property, I tried just putting it at the top of a ZStack
but that would also affect the buttons positions.
I should also mention I am relatively new to Swift
struct photoDetail: View {
let image: realPhoto
@State var textControl: Bool
init(image: realPhoto) {
self.image = image
self.textControl = image.favorited
}
var body: some View {
ZStack {
VStack(alignment: .leading) {
Spacer()
HStack {
Text("created by: \(image.username)")
.padding(5)
.foregroundColor(.white)
.background(Color(.gray).opacity(0.8))
.padding(.leading, 20)
.font(.system(size:20, weight: .semibold))
.padding(.bottom, 50)
.cornerRadius(4)
Spacer()
}.padding(.bottom, 60)
}
VStack {
HStack {
Spacer()
Button {
let imageSaver = ImageSaver()
imageSaver.writeToPhotoAlbum(image: UIImage(data: image.image)!)
} label: {
Image(systemName: "square.and.arrow.down")
.resizable()
.scaledToFit()
.frame(width: 20)
.padding(8)
}
Button {
favoriteToggle()
} label: {
Image(systemName: textControl == false || image.favorited == false ? "heart": "heart.fill")
.resizable()
.scaledToFit()
.frame(width: 25)
.padding(8)
}.padding(.trailing, 30)
}.padding(.top, 100)
Spacer()
}
}.onAppear() {
self.textControl = image.favorited
}.background(
Image(uiImage: UIImage(data: image.image)!)
.resizable()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.scaledToFill()
.clipped()
).ignoresSafeArea()
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
.contentshape(Rectangle())
添加到您的按钮。它应该限制可敲击区域到您的按钮视图Try to add
.contentShape(Rectangle())
to your buttons. It should limit tappable area to your button view