You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI中SwipeActions二次快速滑动时内容尺寸异常增大问题求助

SwiftUI中SwipeActions二次快速滑动时内容尺寸异常增大问题求助

大家好,我最近在做SwiftUI列表滑动操作时遇到了一个棘手的问题,想请教下社区的各位大佬有没有解决思路:

我为了让SwipeActions里的字体和图标尺寸严格贴合设计要求,用UIImage来组合这些内容。正常滑动展开时显示完全没问题,但如果用户快速操作——在滑动关闭的动画还没完全结束时就再次滑动展开——按钮里的内容会莫名其妙地变大;可要是等关闭动画彻底结束后再滑动,尺寸就又恢复正常了。另外我还发现,这个问题似乎只在可滑动内容高度足够时才会复现。

以下是我最初的实现代码:

List {
    Text()
        .frame(height: 80)
        .swipeActions(
            edge: .trailing,
            allowsFullSwipe: allowsFullSwipeTrailing
        ) {
            Button() {
            } label: {
                Label(
                    title: {},
                    icon: { Image(uiImage: combine()) }
                )
            }
        }
}

private func combine() -> UIImage {
    if #available(iOS 16.0, *) {
        let renderer = ImageRenderer(
            content: VStack(spacing: 2) {
                Image(systemName: "calendar")
                    .resizable()
                    .frame(width: 20, height: 20)
                    .foregroundColor(.brown)
                Text("This string is long")
                    .foregroundColor(.accentColor)
                    .frame(width: 80)
            }
            .frame(width: 100)
        )
        return renderer.uiImage ?? UIImage(udsImage: .aFine)!
    } else {
        return UIImage(udsImage: .aFine)!
    }
}

后来我尝试了Benzy Neez的一个解决方案,改用自定义绘制的Image来实现,但问题似乎还是没解决(也可能是我哪里写法有问题?),以下是我尝试后的代码:

List {
    Text("Test")
        .frame(height: 80)
        .swipeActions(
            edge: .trailing,
            allowsFullSwipe: allowsFullSwipeTrailing
        ) {
            Button() {
            } label: {
                Image(
                    size: CGSize(width: 80, height: 80),
                    label: Text("Delete")
                ) { ctx in
                    ctx.draw(
                        Image(systemName: "trash"),
                        at: CGPoint(x: 30, y: 0),
                        anchor: .top
                    )
                    ctx.draw(
                        Text("Delete"),
                        at: CGPoint(x: 30, y: 20),
                        anchor: .top
                    )
                }
            }
        }
}

有没有大佬遇到过类似的问题?或者能帮我看看我的实现哪里有疏漏,应该怎么调整才能避免这种快速操作下的尺寸异常呢?非常感谢大家的帮助🙏

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 09:45:28