IOS 16 Orientation Issues
With IOS 16 all my apps that support multiple orientations are now broken when rotating the device. Apple has broken my apps so many times with stuff like this. It’s so frustrating! Anyway…
Here is how I fixed it for my apps. In my main view controller I listen for orientation changes:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
That has been that way for years… works fine and calls orientationChange
.
- (void)orientationChange:(NSNotification *)note {
UIDevice * device = note.object;
orient = device.orientation;
// found this by searching around github https://github.com/liLeiBest/LZDependencyToolkit/blob/bca976207ef9674632d448e62ac425c22cecbc4f/LZDependencyToolkit/Classes/Category/UIViewController+LZForceRotation.m#L107
// glad that someone figured it out :D
if (@available(iOS 16, *)) {
[self setNeedsUpdateOfSupportedInterfaceOrientations];
[self.navigationController setNeedsUpdateOfSupportedInterfaceOrientations];
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
if (array.count) {
UIWindowScene *scene = (UIWindowScene *)array[0];
UIWindowSceneGeometryPreferencesIOS *geometryperences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:(1 << orient)];
[scene requestGeometryUpdateWithPreferences:geometryperences errorHandler:^(NSError * _Nonnull error) {
NSLog(@"Orientation Error:%@", error);
}];
}
}
[self refreshUI];
}
That big blob checking if IOS 16 is available is now necessary. Within my refreshUI
method I use the width and height of the screen to position things. It seems that [[UIScreen mainScreen] bounds].size.height
now takes a few hundred milliseconds to be updated with the current orientation. Maybe there is some new animation that needs to be disabled, but I have not been able to find it. So, I just delay 200ms and all seems ok:
- (void) refreshUI {
[self delay:0.2 callback:^(void){
UIWindow* window=[[UIApplication sharedApplication] keyWindow];
float t = window.safeAreaInsets.top;
float b = window.safeAreaInsets.bottom;
float l = window.safeAreaInsets.left;
float r = window.safeAreaInsets.right;
H = [[UIScreen mainScreen] bounds].size.height;
W = [[UIScreen mainScreen] bounds].size.width;
// do some layout change stuff using `W` and `H`
}];
}
My delay method just looks like this:
- (void)delay:(double)time callback:(void(^)(void))callback {
double delayInSeconds = time;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), callback);
}
Beware of IOS dev – its riddled with this stuff…