my UIViewController currently only support portrait what do i have to change to support landscape in Objective-C

Asked on 08/02/2024

1 search

To support landscape orientation in your UIViewController in Objective-C, you need to make a few changes to your app's settings and code. Here are the steps:

  1. Update the Info.plist: Ensure that your app's Info.plist file includes support for landscape orientations. Add the following keys if they are not already present:

    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    
  2. Override the Supported Interface Orientations in Your View Controller: In your UIViewController subclass, override the supportedInterfaceOrientations method to specify the orientations your view controller supports:

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
    }
    
  3. Handle Orientation Changes: If you need to handle layout changes when the orientation changes, you can override the viewWillTransitionToSize:withTransitionCoordinator: method:

    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
        [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
        
        [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
            // Handle the orientation change
            [self updateLayoutForNewSize:size];
        } completion:nil];
    }
    
    - (void)updateLayoutForNewSize:(CGSize)size {
        // Update your layout here
    }
    

For more detailed information on handling different device orientations and ensuring your UI adapts correctly, you might find the session What’s new in UIKit helpful, especially the chapter on "Automatic trait tracking" which starts at 06:02.

my UIViewController currently only support portrait what do i have to change to support landscape in Objective-C | Ask WWDC