Rotation issue from the first view (splash) to fir

2019-09-06 21:54发布

问题:

I am working in Xcode 4.5.2, targeting iOS6 for an iPad app, using storyboards and segues. Preamble: my root controller (loaded by the app delegate) is a splash screen with only an image, an upgrade button and an open button. App takes a few seconds to load. I have shouldAutorotate and supportedInterfaceOrientations in all three of my full screen controllers. For rotation notification, I am using the following two methods in my root view controller:

- (void)awakeFromNib
{
    [UIDevice.currentDevice beginGeneratingDeviceOrientationNotifications];
    [NSNotificationCenter.defaultCenter addObserver:self
                                       selector:@selector(orientationChanged:)
                                           name:UIDeviceOrientationDidChangeNotification
                                         object:nil];
}

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation))
    {
        // Landscape
        CGRect rect = _libraryViewController.libraryTableBorder.frame;
        _libraryViewController.libraryTableBorder.frame = rect;
        _libraryViewController.libraryTableBorder.image = [UIImage imageNamed:@"LibraryBorder_L.png"];
    }
    else if (UIDeviceOrientationIsPortrait(deviceOrientation))
    {
        // Portrait
        CGRect rect = _libraryViewController.libraryTableBorder.frame;
        _libraryViewController.libraryTableBorder.frame = rect;
        _libraryViewController.libraryTableBorder.image = [UIImage imageNamed:@"LibraryBorder_P.png"];
    }
}

I have these very same methods in the LibraryViewController and it works perfectly. I have another main view controller (entryView) that has the same methods without the calls for the libraryTableBorder. No matter what rotation the device is in coming from or going to the entry view, the table border swaps out correctly. And, when going from the library to either the entryView or to the splash, the views are correct.

The issue is going from the splash view in landscape to the library. Going to the library in Potrait works fine and the border displayed is the portrait border. But, in landscape, it also displays the portrait border. How can I get the library border to display in landscape when coming from the root view when it is in landscape?

Any help in solving this conundrum would be much appreciated!!!

回答1:

I have found the solution to this issue... viewWillLayoutSubviews

I was prompted by another thread, which pointed to the release notes for iOS6. I added the UIDeviceOrientation and my if/else statement to it. Now the border rotates correctly, regardless of which view I go to or come from!

- (void)viewWillLayoutSubviews
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation))
    {
        // Landscape
        CGRect rect = _libraryTableBorder.frame;
        _libraryTableBorder.frame = rect;
        _libraryTableBorder.image = [UIImage imageNamed:@"LibraryBorder_L.png"];
    }
    else if (UIDeviceOrientationIsPortrait(deviceOrientation))
    {
        // Portrait
        CGRect rect = _libraryTableBorder.frame;
        _libraryTableBorder.frame = rect;
        _libraryTableBorder.image = [UIImage imageNamed:@"LibraryBorder_P.png"];
    }
}

This was sure a frustrating issues for me. Hope this helps someone else!