Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Widget iOS 14 gradient issue

I want to create a gradient color for my widget using custom colors. And I have a problem when I only use two colors, as a result, not one of the colors is applied, but the background turns green!

struct WeatherWidgetMediumView: View {
    
    var gradient: LinearGradient {
        LinearGradient(
            gradient: Gradient(
                colors:
                [
                    Color(red: 96.0/255.0, green: 171.0/255.0, blue: 239.0/255.0),
                    Color(red: 163.0/255.0, green: 230.0/255.0, blue: 244.0/255.0)
                ]),
            startPoint: .top,
            endPoint: .bottom)
    }
    
    var body: some View {
        GeometryReader { geo in
            HStack(alignment: .center) {
                Divider().background(Color.black).padding(.vertical, 16.0).opacity(0.1)
            }
        }
        .background(gradient)
    }
}

enter image description here

But If I added one more color it's looks great.

struct WeatherWidgetMediumView: View {
    let weather: Weather
    
    var gradient: LinearGradient {
        LinearGradient(
            gradient: Gradient(
                colors:
                [
                    Color(red: 96.0/255.0, green: 171.0/255.0, blue: 239.0/255.0),
                    Color(red: 96.0/255.0, green: 171.0/255.0, blue: 239.0/255.0),
                    Color(red: 163.0/255.0, green: 230.0/255.0, blue: 244.0/255.0)
                ]),
            startPoint: .top,
            endPoint: .bottom)
    }
    
    var body: some View {
        GeometryReader { geo in
            HStack(alignment: .center) {
                Divider().background(Color.black).padding(.vertical, 16.0).opacity(0.1)
            }
            Spacer()
        }
        .background(gradient)
    }
}

enter image description here

UPD: Create a GitHub project with this issue

https://github.com/Maxim-Zakopaylov/widgetKitGradientIssue

like image 816
Maxim Zakopaylov Avatar asked Oct 22 '20 13:10

Maxim Zakopaylov


1 Answers

I've faced the exact issue in my app, and I've fixed it by adding .blendMode(.darken) after the background modifier. I hope that would solve your problem too.

like image 188
Faisal AlMaarik Avatar answered Oct 14 '22 08:10

Faisal AlMaarik