按钮下方的Swiftui Tap区域

发布于 2025-02-12 22:39:53 字数 2658 浏览 1 评论 0原文

我的应用中我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯淡〆 2025-02-19 22:39:53

尝试将.contentshape(Rectangle())添加到您的按钮。它应该限制可敲击区域到您的按钮视图

Try to add .contentShape(Rectangle()) to your buttons. It should limit tappable area to your button view

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文