Hi all! My iOS app is in portrait mode but I want ...
# compose-ios
g
Hi all! My iOS app is in portrait mode but I want one particular Composable to be in Landscape (full screen video). On Android I'm using the code below but I can't figure out how to do the same on iOS
Copy code
@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
        }
    }
}
a
Hi! Check
UIViewController.*supportedInterfaceOrientations
or*
UIApplicationDelegate.application(_:supportedInterfaceOrientationsFor:)
API.
g
Thanks @Andrei Salavei I tried that but I struggled to force all screens in portrait mode but for the one screen that I want in landscape
y
@Greg Rami - Are you able to force only one screen in landscape? I'm trying to force one screen to be in landscape with
UIViewController.*supportedInterfaceOrientations*
it is not working
I tried with this code which works in ios swift but when i tried accessing these function are not available to override in compose iosMain shouldAutorotate and supportedInterfaceOrientations not available to override This works even above ios 16
*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]
} }
a
Force rotation is a very tricky thing. In case we are not taking into account non-standard solutions, iOS won't rotate a single screen to the landscape - It will do it with entire app anyway. Also, it depend on the navigation you're using (is it UINavigationController/UITabBarController, or it's child-parent connection). So you have to adjust value of the the
supportedInterfaceOrientations
(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()
i
This is the solution that works for me
Copy code
LaunchedEffect(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)  
        }  
    }  
}
🙇 1
amaze 1