Description
Steps to Reproduce
Running on iOS 10/11
Start an app in portrait mode and invoke:
SystemChrome.setPreferredOrientations([ DeviceOrientation.portraitUp ]);
then invoke
SystemChrome.setPreferredOrientations([ DeviceOrientation.landscapeLeft ]);
Notice that neither the device neither the simulator automatically rotate until you physically rotate the device (and does not rotate at all if the device rotation trigger is locked).
This happens because calling +[UIViewController attemptRotationToDeviceOrientation] here https://github.com/flutter/engine/blob/master/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm#L703 is unfortunately not enough.
I workarounded the issue creating a small platform channel that invokes this code for switching to portrait right before the call to setPreferredOrientations:
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
And the counterpart code for switching to landscape
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
This solved my issue.
I think the current behaviour under iOS is bugged, moreover because my expected behaviour is what is currently happening on the Android counterpart without the need of any workaroud.
I know that the proposed way to fix it looks bad (using key-value coding on a read-only property), but if you search around the Internet, it seems to be the most reliable (and possibly only) way to handle this thing when you have just one view / view controller.
Activity
sroddy commentedon Nov 29, 2017
cc @chinmaygarde
chinmaygarde commentedon Nov 30, 2017
I am not sure why this is not working for you. From my reading of the documentation on
-[UIViewController supportedInterfaceOrientations]
and-[UIViewController shouldAutorotate]
, the responses that theFlutterViewController
makes to UIKit seem to be correct (assuming that the view controller is the top most view controller in the view controller hierarchy).Have you checked whether the supported interface orientations are present in the application's
Info.plist
and that the application or its delegate is not giving conflicting replies in the-[UIApplicationDelegate application:supportedInterfaceOrientationsForWindow:]
method?chinmaygarde commentedon Nov 30, 2017
cc @cbracken
sroddy commentedon Nov 30, 2017
I know they seem right. Unfortunately they don't work.
https://stackoverflow.com/a/19125466
I've been fighting in the past with this issue in the last 1 year and half and so far the only consistent way I've found to deal with this is to check the real device orientation and force it to one of the supported ones if unsupported.
sroddy commentedon Dec 4, 2017
@chinmaygarde @cbracken you can see here a basic example demonstrating the issue:
Notice that:
btastic commentedon Feb 24, 2018
We just noticed that when the device was in landscape and then the flutter app is opened with
The app will open in landscaped regardless of this setting.
CaiJingLong commentedon Jun 8, 2018
In iPad, this method doesn't seem to be implemented.
When I rotated iPad, flutter's UI rotated.
flutter doctor -v
miguelpruivo commentedon Aug 21, 2018
+1
crnaosa commentedon Aug 31, 2018
+1
zoechi commentedon Aug 31, 2018
@miguelpruivo @crnaosa
Using "add reaction" on the initial comment would increase priority while
+1
comments are rather pointless and cause lots of people to get notified for no reason.If you want to get notified just click the "Subscribe" button to the right.
lennartschoch commentedon Oct 11, 2018
I am experiencing the same issue both with the iOS emulator (iPad Pro 9.7, iOS 12) and a physical device (iPad Air 2, iOS 11). I implemented the setPreferredOrientations method inside the main function, but there is no effect noticeable. This is a huge problem when developing with user interfaces which are designed to only work in either portrait or landscape mode, not both. Can we get an update on this?
albo1337 commentedon Oct 24, 2018
@sroddy Could you please share your solution? I'm facing the same issue and have no idea how to fix this on iOS. It seems that you fixed it.
70 remaining items
mofada commentedon Nov 9, 2019
@kanthi0802
mofada commentedon Nov 9, 2019
upgrade your flutter and retry
kanthi0802 commentedon Nov 9, 2019
Not working for me tried with
Flutter (Channel master, v1.10.16-pre.110, on Mac OS X 10.14.1 18B75, locale en-GB).
Should we do anything other than calling
SystemChrome.setPreferredOrientations(
[DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]); ?
ZTPR0035 commentedon Nov 11, 2019
Hello @lennartschoch, I think this will solve your issue , I tried it and it work on both platforms,
return RotatedBox(
quarterTurns: 1,
child: Scaffold(),);
cbracken commentedon Nov 11, 2019
Sounds like this is resolved based on other reports.
@lennartschoch please let us know if @ZTPR0035's suggestion doesn't work for you.
mirkopoloni commentedon Dec 20, 2019
Working fine on iPhone but still having problems on iPad, am I the only one?
Channel master, v1.13.4-pre.12, on Mac OS X 10.15.1
UPDATE 13 January
I just found out that this can't actually work on iPad without some limitations, it isn't a bug... sorry if I didn't find it out before writing my comment.
As a reference for other people, more infos here:
#27235 (comment)
https://api.flutter.dev/flutter/services/SystemChrome/setPreferredOrientations.html
hawkinsjb1 commentedon Jul 8, 2020
Is the only solution to this really to use a RotatedBox as @ZTPR0035 mentioned? This doesn't properly change the screen's size like a native orientation change. I'm having lots of overflow errors trying to use that solution
EDIT: Nevermind orientation is properly getting updated, my WillPopScope wasn't being triggered because I wasn't using maybePop(), duh..
pisces116 commentedon Sep 15, 2020
Why this issue is not resolved but is marked as resolved
SaidiAli commentedon Jan 13, 2021
The issue is closed but I think i can add this just incase. I am just learning flutter and in a lecture by Maximiliano of academind, he points out that for the issue at hand to work you need to add a
WidgetFlutterBinding.ensureInitialised()
.Like:
WidgetFlutterBinding.ensureInitialised(); SystemChrome.setPrefferedOrientation();
yubaokang commentedon Jan 18, 2021
then same
github-actions commentedon Aug 7, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -v
and a minimal reproduction of the issue.