Greg Rami
03/25/2025, 11:47 AM@Composable
fun LockScreenOrientation(orientation: Int) {
val context = LocalContext.current
DisposableEffect(orientation) {
val activity = context.findActivity() ?: return@DisposableEffect onDispose {}
val originalOrientation = activity.requestedOrientation
activity.requestedOrientation = orientation
onDispose {
// restore original orientation when view disappears
activity.requestedOrientation = originalOrientation
}
}
}
Andrei Salavei
03/26/2025, 11:47 PMUIViewController.*supportedInterfaceOrientations
or* UIApplicationDelegate.application(_:supportedInterfaceOrientationsFor:)
API.Greg Rami
03/27/2025, 12:08 AMYuvaraj
03/27/2025, 3:30 AMUIViewController.*supportedInterfaceOrientations*
it is not working
Yuvaraj
03/27/2025, 3:47 AM*class* ViewController: UIViewController {
required
*init*?(coder: NSCoder) {
*self*.screenRecorderIsActive = *true*
*super*.init(coder: coder)
}
override
*var* shouldAutorotate: Bool{
return
false
}
*var* screenRecorderIsActive: Bool {
*didSet* {
setNeedsUpdateOfSupportedInterfaceOrientations()
}
}
override
*var* supportedInterfaceOrientations: UIInterfaceOrientationMask {
*if* screenRecorderIsActive {
*return* [.landscape] // For example, or a variable representing the orientation when the condition was set
}
*return* [.all]
}
}Andrei Salavei
03/27/2025, 8:37 AMsupportedInterfaceOrientations
(and ensure this value is propagated down to the root controller of the UIWindow) or application(_:supportedInterfaceOrientationsFor:)
and notify iOS about this change:
https://developer.apple.com/documentation/uikit/uiviewcontroller/attemptrotationtodeviceorientation()
https://developer.apple.com/documentation/uikit/uiviewcontroller/setneedsupdateofsupportedinterfaceorientations()Ismail
03/27/2025, 4:29 PMLaunchedEffect(Unit) {
setInterfaceOrientation(UIInterfaceOrientationMaskLandscapeRight)
}
fun setInterfaceOrientation(orientationMask: UIInterfaceOrientationMask) {
val scenes = UIApplication.sharedApplication.connectedScenes
val scene = scenes.firstOrNull { it is UIWindowScene } as? UIWindowScene
if (scene == null) {
println("No active UIWindowScene found. Orientation update skipped.")
return
}
val geometryPreferences = UIWindowSceneGeometryPreferencesIOS(orientationMask)
scene.requestGeometryUpdateWithPreferences(geometryPreferences) { error ->
if (error != null) {
println(error.localizedDescription)
}
}
}