0
votes

I'm working on an iPad app that start with a splash screen and then moves to the login screen. My app should support all the orientation, and iOS 4.3 and later. To do that, I added the four orientations in the plist, and the following code in the app delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    [self.window makeKeyAndVisible]; 
    // Override point for customization after application launch
    SplashViewController *aController = [[SplashViewController alloc] initWithNibName:@"SplashView" bundle:nil];
    self.mainViewController = aController;
    [aController release];

    mainViewController.view.frame = [UIScreen mainScreen].bounds;// no effect
    [window setFrame:[[UIScreen mainScreen] bounds]]; //no effect
    //[window addSubview:[mainViewController view]];
    [window setRootViewController:mainViewController];

    return YES;
}

- (NSUInteger)application:(UIApplication *)application     supportedInterfaceOrientationsForWindow:(UIWindow *)window{

    return UIInterfaceOrientationMaskAll;
}

In the splash screen

- (void) loadView {
    [super loadView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
}
- (BOOL)shouldAutorotate{
    return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}
- (void) orientationChanged
{
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            NSLog(@"Portrait");
    else
            NSLog(@"Landscape");

}

Here I get the correct orientation but when rotating, I get an inverted result, I get landscape for portrait and portrait for landscape. I tried to convert the code like this:

- (void) orientationChanged
{
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            NSLog(@"Landscape");
    else
            NSLog(@"Portrait");

}

I get the first result wrong, after that the result is correct. can you help? note that I have put some uielement to test and the test come with the same result. Thank you

1

1 Answers

0
votes

I managed to solve this matter after two days of searching and testing, none of the answers in the forum was complete, and here is how I solved:

//this for the orientation
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self orientationChanged];
}

// here is the tricky thing, when startup you should have a special function for the orientation other than the orientationchanged method, and must be called here in viewDidAppear, otherwise it won't work
-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self StartUpOrientation];
}
#pragma mark -
#pragma mark Orientation Methods

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (BOOL)shouldAutorotate {
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;

}

- (void) orientationChanged {
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation != UIInterfaceOrientationLandscapeLeft && interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
        [self.view setBounds:CGRectMake(0, 0, 1024, 748)];
        // your code here
    }
    else {
        [self.view setBounds:CGRectMake(0, 0,768,1004)];
        //your code here
    }
}

- (void) StartUpOrientation {
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation != UIInterfaceOrientationLandscapeLeft && interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
        [self.view setBounds:CGRectMake(0, 0,768,1004)];
        // your code here
    }
else {
        [self.view setBounds:CGRectMake(0, 0, 1024, 748)];
        // your code here
    }
}

hope it will help someone someday