Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: fullScreenCover with no animation?

Tags:

swiftui

I have this view:

struct TheFullCover: View {
    
    @State var showModal = false
    
    var body: some View {
        
        
        Button(action: {
            showModal.toggle()
        }) {
            Text("Show Modal")
                .padding()
                .foregroundColor(.blue)
            
        }
        .background(Color(.white))
        .overlay(
            RoundedRectangle(cornerRadius: 10)
                .stroke(.red, lineWidth:1)
        )
        .fullScreenCover(isPresented: $showModal, onDismiss: {
            
        }, content: {
            VStack {
                Text("Here I am")
                TheFullCover()
            }
        })
        
    }
    
    
}

Every time I press the Button, the modal screen comes up fullscreen. All works great.

Question:

How do I disable the slide up animation? I want the view to be presented immediately fullscreen without animating to it.

Is there a way to do that?

like image 337
zumzum Avatar asked Sep 14 '25 04:09

zumzum


1 Answers

AFAIK the proper to do it as of today is using transaction https://developer.apple.com/documentation/swiftui/transaction

var transaction = Transaction()
transaction.disablesAnimations = true
withTransaction(transaction) {
   showModal.toggle()
}

I also created a handy extension for this:

extension View {
    func withoutAnimation(action: @escaping () -> Void) {
        var transaction = Transaction()
        transaction.disablesAnimations = true
        withTransaction(transaction) {
            action()
        }
    }
}

which can be used like this:

withoutAnimation {
    // do your thing
}
like image 164
asamoylenko Avatar answered Sep 16 '25 18:09

asamoylenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!