开发者

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

开发者 https://www.devze.com 2023-01-12 14:19 出处:网络
I have a helloController which is a UIViewController, if I rotate the device, I want it change it to load a new nib \"hello开发者_如何学运维Horizontal.xib\", how can I do? thank you.You could you some

I have a helloController which is a UIViewController, if I rotate the device, I want it change it to load a new nib "hello开发者_如何学运维Horizontal.xib", how can I do? thank you.


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];
}


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];
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号