Yes same issue I am also facing. I does not lock the orientation, when I was trying to rotate it is rotating
I am just try to add 2 button in screen for portrait and landscape. when we clicks portrait it should be portrait mode same when we click landscape it should be landscape. its working fine in that case. but when I rotate the device screen is display in both the mode , As per my logic it should be lock.
Swift Helper Class I am using in objective c code
@objc public class HelperSwift: UIViewController {
@objc static func setDeviceOrientation(orientation: UIInterfaceOrientationMask , controller:UIViewController) {
if #available(iOS 16.0, *) {
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientation)) { error in
print("orientation error", error.localizedDescription)
}
} else {
UIDevice.current.setValue(orientation.toUIInterfaceOrientation.rawValue, forKey: "orientation")
}
}
}
extension UIInterfaceOrientationMask {
var toUIInterfaceOrientation: UIInterfaceOrientation {
switch self {
case .portrait:
return UIInterfaceOrientation.portrait
case .portraitUpsideDown:
return UIInterfaceOrientation.portraitUpsideDown
case .landscapeRight:
return UIInterfaceOrientation.landscapeRight
case .landscapeLeft:
return UIInterfaceOrientation.landscapeLeft
default:
return UIInterfaceOrientation.unknown
}
}
}
ViewController
@implementation ViewController
[super viewDidLoad];
}
-(IBAction)landScape:(id)sender {
orientation = UIInterfaceOrientationMaskLandscapeLeft;
[HelperSwift setDeviceOrientationWithOrientation:orientation controller:self];
}
-(IBAction)portrait:(id)sender {
orientation = UIInterfaceOrientationMaskPortrait;
[HelperSwift setDeviceOrientationWithOrientation:orientation controller:self];
}
- (void)setNeedsUpdateOfSupportedInterfaceOrientations {
NSLog(@"Called rotate the device and orientation is change");
}
// this is deprecated
-(BOOL)shouldAutorotate
{
UIDeviceOrientation orientationA = [[UIDevice currentDevice] orientation];
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
NSUInteger supportedOrientations = orientation;
return supportedOrientations;
}