0

I'm trying to make my app using SWRevealViewController show the side panel automatically when the device is turned to landscape orientation, and I can make it do so when the app initially opens but not after that. Basically I'm trying to make it behave somewhat like the Mail app on the iPad except that you can still manually close the side panel on landscape mode. I tried this in the AppDelegate without success:

#import "AppDelegate.h"
#import "SWRevealViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    SWRevealViewController *revealViewController = (SWRevealViewController *)self.window.rootViewController;

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == UIInterfaceOrientationPortrait) {

    }   
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {

        if (revealViewController.frontViewPosition == FrontViewPositionLeft) {
            [revealViewController revealToggleAnimated:YES];           
        }
    }
    return YES;
}

Can anyone please tell me what I should be doing instead?

4

1 回答 1

1

我终于弄明白了。一个更简单的方法是简单地将以下代码添加到前视图控制器:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    [self openSidePanel:interfaceOrientation];
}

- (void)openSidePanel:(UIInterfaceOrientation)orientation
{
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        [self.revealViewController setFrontViewPosition:FrontViewPositionRight animated:YES];
    }
    else {
        [self.revealViewController setFrontViewPosition:FrontViewPositionLeft animated:YES];
    }

}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self openSidePanel:toInterfaceOrientation];
}

现在,当我将 iPad 旋转到横向模式时,侧面板会自动打开;当我将其旋转回纵向模式时,它会关闭侧面板。

于 2014-07-11T07:47:23.117 回答