Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a new nib when I rotate the device?

I have a helloController which is a UIViewController, if I rotate the device, I want it change it to load a new nib "helloHorizontal.xib", how can I do? thank you.

like image 813
DNB5brims Avatar asked Aug 18 '10 08:08

DNB5brims


2 Answers

You could you something like this, (I dont have xcode handy so this code might not be completely accurate)

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if((interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)){
WhatYourNewViewClassISCAlled* newView = [[WhatYourNewViewClassISCAlled alloc] initWithNibName:@"NIBNAME" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:newView animated:YES];
}
like image 115
octermircty Avatar answered Sep 24 '22 02:09

octermircty


This is the correct way, I believe. I'm using it in my apps and it works perfectly

  1. triggers on WILL rotate, not SHOULD rotate (waits until the rotate anim is about to start)
  2. uses the Apple naming convention for landscape/portrait files (Default.png is Default-landscape.png if you want Apple to auto-load a landscape version)
  3. reloads the new NIB
  4. which resets the self.view - this will AUTOMATICALLY update the display
  5. and then it calls viewDidLoad (Apple will NOT call this for you, if you manually reload a NIB)

(NB stackoverflow.com requires this sentence here - there's a bug in the code formatter)

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if( UIInterfaceOrientationIsLandscape(toInterfaceOrientation) )
    {
        [[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"%@-landscape", NSStringFromClass([self class])] owner:self options:nil];

        [self viewDidLoad];
    }
    else
    {
        [[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"%@", NSStringFromClass([self class])] owner:self options:nil];

        [self viewDidLoad];
    }
}
like image 41
Adam Avatar answered Sep 23 '22 02:09

Adam