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

SwiftUI中为缩放1.7的圆形添加与背景一致渐变时出现全黑异常的技术问询

Fixing Black Circle When Applying Linear Gradient in SwiftUI

I have this SwiftUI login screen code, and I want to apply the same linear gradient as the background to the circle with scale 1.7. But when I use .background(LinearGradient(...)) on that circle, it turns completely black. How can I fix this?

My code:

import SwiftUI
struct LoginScreen: View {
    var body: some View {
        NavigationView {
            ZStack {
                LinearGradient(colors: [.blue, .black], startPoint: .topLeading, endPoint: .bottomTrailing)
                Circle()
                    .scale(1.3)
                    .foregroundColor(.white.opacity(0.6))
                Circle()
                    .scale(1.7)
                    .foregroundColor(.white.opacity(0.3))
            }
            .navigationBarHidden(true)
        }
        .edgesIgnoringSafeArea(.all)
    }
}

The issue happens because a default Circle has a solid black fill — when you add a background modifier, it sits behind this black fill, so you can't see the gradient at all. Instead of using background, you need to apply the gradient directly as the fill of the circle, while maintaining the transparency effect you had before.

Here are two straightforward ways to fix this:

Option 1: Use fill() with a transparent gradient

Adjust the gradient colors to include the 0.3 opacity you used earlier, then apply it as the circle's fill:

import SwiftUI
struct LoginScreen: View {
    var body: some View {
        NavigationView {
            ZStack {
                // Background gradient
                LinearGradient(colors: [.blue, .black], startPoint: .topLeading, endPoint: .bottomTrailing)
                
                Circle()
                    .scale(1.3)
                    .foregroundColor(.white.opacity(0.6))
                
                // Modified circle with matching gradient + transparency
                Circle()
                    .scale(1.7)
                    .fill(
                        LinearGradient(
                            colors: [.blue.opacity(0.3), .black.opacity(0.3)],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        )
                    )
            }
            .navigationBarHidden(true)
        }
        .edgesIgnoringSafeArea(.all)
    }
}

Option 2: Use foregroundStyle() + opacity()

If you want to reuse the exact same gradient (without modifying color opacities), you can define the gradient as a variable, then apply it with foregroundStyle and add an opacity modifier:

import SwiftUI
struct LoginScreen: View {
    // Reusable gradient variable
    private let backgroundGradient = LinearGradient(
        colors: [.blue, .black],
        startPoint: .topLeading,
        endPoint: .bottomTrailing
    )
    
    var body: some View {
        NavigationView {
            ZStack {
                backgroundGradient
                
                Circle()
                    .scale(1.3)
                    .foregroundColor(.white.opacity(0.6))
                
                Circle()
                    .scale(1.7)
                    .foregroundStyle(backgroundGradient)
                    .opacity(0.3) // Apply the transparency here
            }
            .navigationBarHidden(true)
        }
        .edgesIgnoringSafeArea(.all)
    }
}

Both approaches will give you the semi-transparent gradient circle you want, matching the background's gradient direction and colors, without the circle turning black.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 22:08:12