For UIKit to disable the device rotation animation you could override the viewWillTransitionToSize method. Disable orientation change rotation animation But what would be the best way to achieve this in SwiftUI?
Asked
Active
Viewed 855 times
10
-
did you find any solution to this issue? I'm having the same problem – Andrei Matei Nov 10 '21 at 07:49
-
Bump. Any news on this? Been looking for a solution for this – Sylar Nov 17 '21 at 14:08
1 Answers
1
We can do this almost in the same way - create a helper view controller with animation blocker and use it inside background as representable.
Tested with Xcode 13.4 / iOS 15.5
struct HelperView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> some UIViewController {
OrientationHandler()
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
}
class OrientationHandler: UIViewController {
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: nil) { _ in
UIView.setAnimationsEnabled(true)
}
UIView.setAnimationsEnabled(false)
super.viewWillTransition(to: size, with: coordinator);
}
}
}
and usage:
WindowGroup {
ContentView()
.background(HelperView()) // << here !!
}

Asperi
- 228,894
- 20
- 464
- 690