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

SwiftUI中使用.transition时图片无法显示的问题

Hey there! Let's troubleshoot why your image isn't appearing when you tap that summon button. I see a couple of potential issues that could be causing this:

1. Case-sensitive image asset name

Swift is strict about case sensitivity. If your image in Assets.xcassets is named something like "him" (all lowercase) instead of "HIM" (uppercase), Image("HIM") won't locate it. Double-check your asset catalog to ensure the name matches exactly—capitalization and all.

2. Missing animation wrapper for state changes

Your .scale transition might not trigger correctly because you're toggling showdeatail without wrapping the state change in an animation block. SwiftUI needs explicit instruction to animate view changes tied to state updates. Update your button action like this:

Button(action: {
    withAnimation {
        showdeatail.toggle()
    }
}) {
    Text("S U M M O N\nH I M")
}

3. Image asset not properly linked to your target

Occasionally, images get added to the project folder but aren't included in your app's target. To fix this:

  • Select your image in Assets.xcassets
  • Open the Inspector panel (right side of Xcode)
  • Under "Target Membership", make sure your app's target is checked

4. Minor indentation fix (for readability)

Your current code has modifiers for the Image indented incorrectly—while it might still run, aligning modifiers with the view makes the code easier to maintain:

if showdeatail {
    Image("HIM")
        .frame(width: 100, height: 100)
        .transition(.scale)
}

Here's the full corrected code putting all these fixes together:

struct BEHOLD: View {
    @State private var showdeatail = false
    var body: some View {
        VStack {
            Button(action: {
                withAnimation {
                    showdeatail.toggle()
                }
            }) {
                Text("S U M M O N\nH I M")
            }
            .frame(width: 100, height: 50)
            .background(Color.red)
            .cornerRadius(10)
            
            if showdeatail {
                Image("HIM")
                    .frame(width: 100, height: 100)
                    .transition(.scale)
            }
        }
    }
}

Start with checking the image asset name first—that's the most common culprit here!

内容的提问来源于stack exchange,提问作者Superbia

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 18:19:08