How to Properly Handle Device Orientation Changes with UIViewController in Objective-C?
I've been struggling with this for a few days now and could really use some help. I'm facing issues with handling device orientation changes in my Objective-C app. I have a `UIViewController` where I'm trying to update the layout of my subviews based on the current orientation. However, the layout doesn't seem to refresh correctly when I rotate the device. Here's a simplified version of my code: ```objective-c - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) { // Update layout for landscape self.mySubview.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 2); } else { // Update layout for portrait self.mySubview.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height); } } ``` Even though I'm calling `viewWillLayoutSubviews`, the layout doesn't seem to adjust until I manually trigger a layout pass. I tried calling `[self.view setNeedsLayout];`, but it didn't help. I've also implemented `- (void)deviceOrientationDidChange:(NSNotification *)notification` to handle the orientation change, but the UI still doesn't update accordingly. Here's what that looks like: ```objective-c - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; } - (void)deviceOrientationDidChange:(NSNotification *)notification { [self.view setNeedsLayout]; } ``` I verified that my notification is firing when I rotate the device, but the layout remains unchanged. I'm using Xcode 14.1 and targeting iOS 16.0. Does anyone know how to ensure the layout updates correctly upon orientation changes? Any best practices or suggestions would be greatly appreciated! I'm working on a application that needs to handle this. Thanks in advance!