Thursday, September 28, 2023

ios – SwiftUI glitch / bug – standing bar not displaying when utilizing AVPlayer inside LazyVStack and ScrollView


I am making an attempt to determine why the standing bar would not present when utilizing AVPlayer inside LazyVStack and ScrollView for a lot of hours now so I made a minimal instance that simply must be copied and pasted:

import SwiftUI
import AVKit

struct ViewOffsetKey: PreferenceKey {
    typealias Worth = CGFloat
    static var defaultValue = CGFloat.zero
    static func cut back(worth: inout Worth, nextValue: () -> Worth) {
        worth += nextValue()
    }
}

struct ContentView: View {
    @State var toolbarHeight: CGFloat = 64
    @State var freeScreenHeight: CGFloat = 0
    @State var toolbarOffset: CGFloat = 0
    @State var lastScrollOffset: CGFloat = 0
    @State var startChecking: Bool = false

    @State var participant = AVPlayer()
    var videoUrl: String = "someExternal1:1ratioVideo"


    init() {
        UIScrollView.look().bounces = false
    }
    
    var physique: some View {
             
        ZStack(alignment: .main){

            VStack{
                
                HStack(spacing: 10){

                    Textual content("toolbar")

                    Spacer()

                }
                .padding(.horizontal, 12.0)
                .body(top: toolbarHeight)
                .background(Coloration.inexperienced)
                .padding(.backside, toolbarOffset)
                .offset(y: toolbarOffset)
                .zIndex(2)
                
                Spacer()
            }
            .clipped()
            .zIndex(2)
            

            ScrollView(.vertical, showsIndicators: false){

                LazyVStack(spacing: 0){
                    
                    VStack{

                    }
                    .body(width: spherical(UIScreen.principal.bounds.width / 1.5), top: toolbarHeight + 8)

                    ForEach(1..<5) { index in
                        VideoPlayer(participant: participant)
                            .onAppear{
                                startChecking = true
                            }
                            .body(width: UIScreen.principal.bounds.width, top: UIScreen.principal.bounds.width)
                                        .padding(.backside, 10)
                        
                    }
                }
                .background(GeometryReader {
                    Coloration.clear.choice(key: ViewOffsetKey.self,
                        worth: -$0.body(in: .named("scroll")).origin.y)
                })
                .onPreferenceChange(ViewOffsetKey.self) {
                    
                    if(startChecking){
                        
                        if($0 > lastScrollOffset){

                            let howMuch = $0 - lastScrollOffset

                            if((toolbarOffset - howMuch) < -toolbarHeight){
                                toolbarOffset = -toolbarHeight
                            }else{
                                toolbarOffset -= howMuch
                            }

                        }else{

                            let howMuch = lastScrollOffset - $0

                            if((toolbarOffset + howMuch) > 0){
                                toolbarOffset = 0
                            }else{
                                toolbarOffset += howMuch
                            }
                        }
                    }

                    lastScrollOffset = $0
                }
            }
            .body(top: freeScreenHeight)
        

        }
        .onAppear() {
                participant = AVPlayer(url: URL(string: videoUrl)!)
        }
        .preferredColorScheme(.darkish)
        .background(
            GeometryReader { proxy in

                Coloration.clear
                    .onAppear {
                         
                        freeScreenHeight = UIScreen.principal.bounds.top - proxy.safeAreaInsets.high - proxy.safeAreaInsets.backside
                    }
            }
        )
        .clipped()
    }
}

What could possibly be the rationale for this bug?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

The Obtain: fusion energy’s future, and robotic working

There’s a joke about fusion energy that at all times comes up when individuals begin speaking concerning the expertise. It goes like...

Constructing a sustainable future

The way forward for the development business depends on sustainable renewable power options and eco-friendly practices. New properties and industrial websites must be...

Distributed ZTNA permits easy and scalable safe distant entry to OT property

Zero belief community entry (ZTNA) is the best various to mobile gateways and VPN options for distant entry.However in OT environments, ZTNA must...