According to the iOS & iPadOS 16 Beta 3 Release Notes:- Attempting to set an orientation on UIDevice via setValue:forKey:
isn’t supported and no longer works. Instead, they say use: preferredInterfaceOrientationForPresentation.
In my case, force view controller orientation is not working in iOS 16 beta either by using preferredInterfaceOrientationForPresentation
or requestGeometryUpdate
.
Previously, UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
was working fine.
8
Answers
I noticed my issue seems like resolved by calling method below:
[UIViewController setNeedsUpdateOfSupportedInterface
You may give it a try.
setValue:forKey
is a method of oldNSObject
(NSKeyValueCoding
). It’s not official documented and supported byUIDevice
class. Using it is considering using a private api. Apple can terminate it anytime they want.My problem with my code below is that I’m trying to do it when closing a modal view and the view under it are not updated quick enough. If I put the requestGeometryUpdate on a separate button then when I close the view it work.
Apple released new API which is replaced with setValue:forKey:"orientation".
Apple update
But I am having problem about
UIDevice.orientationDidChangeNotification
, it is not workingIt works for me.
In AppDelegate,
In view controller,
In Helper,
It works for me:
How to use it?
Just call it on your UIViewController:
EDIT
More completed solution:
How to use it:
Following Apple’s documentation you need to
https://developer.apple.com/documentation/uikit/uiwindowscene/3975944-requestgeometryupdate/
So using the code in the example, you can change the way we set the orientation in our views using
requestGeometryUpdate
and using as wellsetNeedsUpdateOFSupportedInterface
I tried all above solutions seem they’re not 100% percent working. After through this post
https://developer.apple.com/forums/thread/707735 i got the hint. Let’s try this below code. It’s worked for me.