Close Menu
  • Home
  • AI
  • Big Data
  • Cloud Computing
  • iOS Development
  • IoT
  • IT/ Cybersecurity
  • Tech
    • Nanotechnology
    • Green Technology
    • Apple
    • Software Development
    • Software Engineering

Subscribe to Updates

Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

    What's Hot

    Building Custom Claude Skills For Repeatable AI Workflows

    March 28, 2026

    Do AI Coding Assistants Powered by LLMs Reduce the Need for Programmers?

    March 28, 2026

    6-Channel Piezo Driver for Precision Actuators & Transducers

    March 28, 2026
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    Big Tee Tech Hub
    • Home
    • AI
    • Big Data
    • Cloud Computing
    • iOS Development
    • IoT
    • IT/ Cybersecurity
    • Tech
      • Nanotechnology
      • Green Technology
      • Apple
      • Software Development
      • Software Engineering
    Big Tee Tech Hub
    Home»iOS Development»ios – How do I get a blending mode to apply to my ScrollView while not affecting the background on latest SwiftUI?
    iOS Development

    ios – How do I get a blending mode to apply to my ScrollView while not affecting the background on latest SwiftUI?

    big tee tech hubBy big tee tech hubSeptember 25, 2025002 Mins Read
    Share Facebook Twitter Pinterest Copy Link LinkedIn Tumblr Email Telegram WhatsApp
    Follow Us
    Google News Flipboard
    ios – How do I get a blending mode to apply to my ScrollView while not affecting the background on latest SwiftUI?
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    The masking approach could work for scroll views too, if you track the frames of each subview of the scroll view. You could use a struct like this:

    struct ScrollContentFrame: Identifiable {
        let id: AnyHashable
        let frame: Anchor
        let shape: AnyShape
    }
    

    I’m keeping things very general here by using AnyHashable and AnyShape. If this is just a one-off effect you want to do, I would recommend using an appropriate ID type, and if the shape is known and constant, remove shape.

    Next, write a preference key that collects an array of these,

    struct ScrollContentFramesKey: PreferenceKey {
        static var defaultValue: [ScrollContentFrame] { [] }
        
        static func reduce(value: inout [ScrollContentFrame], nextValue: () -> [ScrollContentFrame]) {
            value.append(contentsOf: nextValue())
        }
    }
    

    Set the preference key using an anchorPreference,

    .anchorPreference(key: ScrollContentFramesKey.self, value: .bounds) { anchor in
        [
            ScrollContentFrame(
                id: i,
                frame: anchor,
                shape: AnyShape(RoundedRectangle(cornerRadius: 16))
            )
        ]
    }
    

    The color-blended linear gradient can be added as an overlayPreferenceValue instead of ZStack.

    .overlayPreferenceValue(ScrollContentFramesKey.self) { scrollContentFrames in
        LinearGradient(
            colors: [.blue, .orange],
            startPoint: .topLeading,
            endPoint: .bottomTrailing
        )
        .allowsHitTesting(false)
        .mask {
            GeometryReader { proxy in
                ForEach(scrollContentFrames) { content in
                    let frame = proxy[content.frame]
                    content.shape
                        .frame(width: frame.width, height: frame.height)
                        .position(x: frame.midX, y: frame.midY)
                }
            }
        }
        .blendMode(.color)
        .ignoresSafeArea()
    }
    

    Full code for the view:

    struct ContentView: View {
        var body: some View {
            ZStack {
                LinearGradient(
                    colors: [.purple, .pink],
                    startPoint: .top,
                    endPoint: .bottom
                )
                .ignoresSafeArea()
                ScrollView {
                    LazyVStack(spacing: 20) {
                        ForEach(0..<10) { i in
                            RoundedRectangle(cornerRadius: 16)
                                .fill(.white)
                                .frame(height: 120)
                                .overlay(
                                    Text("Card \(i)")
                                        .font(.headline)
                                        .foregroundColor(.red)
                                )
                                .anchorPreference(key: ScrollContentFramesKey.self, value: .bounds) { anchor in
                                    [
                                        ScrollContentFrame(
                                            id: i,
                                            frame: anchor,
                                            shape: AnyShape(RoundedRectangle(cornerRadius: 16))
                                        )
                                    ]
                                }
                                .padding(.horizontal)
                        }
                    }
                    .padding(.vertical, 50)
                }
                .background(Color.clear)
                .overlayPreferenceValue(ScrollContentFramesKey.self) { scrollContentFrames in
                    LinearGradient(
                        colors: [.blue, .orange],
                        startPoint: .topLeading,
                        endPoint: .bottomTrailing
                    )
                    .allowsHitTesting(false)
                    .mask {
                        GeometryReader { proxy in
                            ForEach(scrollContentFrames) { content in
                                let frame = proxy[content.frame]
                                content.shape
                                    .frame(width: frame.width, height: frame.height)
                                    .position(x: frame.midX, y: frame.midY)
                            }
                        }
                    }
                    .blendMode(.color)
                    .ignoresSafeArea()
                }
            }
        }
    }
    



    Source link

    affecting Apply background blending iOS Latest mode ScrollView SwiftUI
    Follow on Google News Follow on Flipboard
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Copy Link
    tonirufai
    big tee tech hub
    • Website

    Related Posts

    ios – Is using DispatchSemaphore in applicationWillTerminate to complete a network request safe for App Store review?

    March 28, 2026

    5+ Things to Know About the Siri Chatbot Coming in iOS 27

    March 28, 2026

    ios – What is the correct way to initialize a Data property in a model object in SwiftData / CloudKit?

    March 27, 2026
    Add A Comment
    Leave A Reply Cancel Reply

    Editors Picks

    Building Custom Claude Skills For Repeatable AI Workflows

    March 28, 2026

    Do AI Coding Assistants Powered by LLMs Reduce the Need for Programmers?

    March 28, 2026

    6-Channel Piezo Driver for Precision Actuators & Transducers

    March 28, 2026

    Best Amazon Spring Sale Deals: We’re Live Tracking 2026’s Biggest Discounts

    March 28, 2026
    About Us
    About Us

    Welcome To big tee tech hub. Big tee tech hub is a Professional seo tools Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of seo tools, with a focus on dependability and tools. We’re working to turn our passion for seo tools into a booming online website. We hope you enjoy our seo tools as much as we enjoy offering them to you.

    Don't Miss!

    Building Custom Claude Skills For Repeatable AI Workflows

    March 28, 2026

    Do AI Coding Assistants Powered by LLMs Reduce the Need for Programmers?

    March 28, 2026

    Subscribe to Updates

    Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

      • About Us
      • Contact Us
      • Disclaimer
      • Privacy Policy
      • Terms and Conditions
      © 2026 bigteetechhub.All Right Reserved

      Type above and press Enter to search. Press Esc to cancel.