SwiftUI remove transition of fullscreen cover

Viewed 1971

Is there any way in how to remove the animation/transition of the fullscreen cover? This is my code:

let contentView = UIHostingController(rootView: ContentView())
    override func viewDidLoad() {
        super.viewDidLoad()
        configureBackgroundGradient()
        addChild(contentView)
        view.addSubview(contentView.view)
        setupContraints()
 }


struct ContentView: View {
    var body: some View {
        EmptyView().fullScreenCover(isPresented: /*@START_MENU_TOKEN@*/.constant(true)/*@END_MENU_TOKEN@*/, content: {
            FullScreenView.init()
        })
    }

Thanks for your help!

2 Answers

Use open class func setAnimationsEnabled(_ enabled: Bool) for disabling animation for the whole app.

In your case, you just needed to disable animation during the fullscreen present and start again after the present.

Here is the possible solution

override func viewDidLoad() {
    super.viewDidLoad()
    configureBackgroundGradient()
    UIView.setAnimationsEnabled(false) //<== Disable animation for whole app
    addChild(contentView)
    view.addSubview(contentView.view)
    DispatchQueue.main.async {
        UIView.setAnimationsEnabled(true) //<== Again enable animation for whole app
    }
    setupContraints()
}


You can also write this inside the ContentView instead of viewDidLoad

struct ContentView: View {
    @State private var isPresent: Bool = false{
        willSet {
            UIView.setAnimationsEnabled(false) //<== Disable animation for whole app
        } didSet {
            DispatchQueue.main.async {
                UIView.setAnimationsEnabled(true) //<== Again enable animation for whole app
            }
        }
    }
    var body: some View {
        EmptyView().fullScreenCover(isPresented: $isPresent, content: {
            FullScreenView.init()
        })
        .onAppear() {
            isPresent = true
        }
    }
}



You can also use this extension.

extension View {
    func withoutAnimation(_ work: @escaping () -> Void) {
        UIView.setAnimationsEnabled(false) //<== Disable animation for whole app
        work()
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            UIView.setAnimationsEnabled(true) //<== Again enable animation for whole app
        }
    }
}

usage:

struct ContentView: View {
    @State private var isPresent: Bool = false
    
    var body: some View {
        EmptyView().fullScreenCover(isPresented: $isPresent, content: {
            FullScreenView.init()
        })
        .onAppear() {
            withoutAnimation {
                isPresent = true
            }
        }
    }
}
Related