Переключение видов в соответствии с ориентацией устройства - PullRequest
2 голосов
/ 03 марта 2012

Я пытаюсь сделать это: в приложении «Мой дом», если я переведу устройство в альбомный режим, я покажу справочную информацию.Пока все работает нормально, но если я нажимаю кнопку, чтобы перейти к другому виду, то я возвращаюсь в «Домой» и помещаю устройство в альбомную ориентацию ... Я вижу все неправильно.

Здесьдва изображения, чтобы попытаться объяснить проблему:

неправильный пейзаж неправильный портрет

Вот мой код ViewController.m:

#import "ViewController.h"

@implementation ViewController
@synthesize portraitView, landscapeView;

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

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


// Do any additional setup after loading the view, typically from a nib.
}


-(void)orientationChanged:(NSNotification *)object{
UIDeviceOrientation deviceOrientation = [[object object] orientation];

    if (deviceOrientation == UIInterfaceOrientationPortrait )
{
    self.view = self.portraitView;        
}
else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation ==    
UIInterfaceOrientationLandscapeRight)
{
    self.view = self.landscapeView;    
}
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:   
(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
    return YES;
}  
}

@end

Я надеюсь, вы можете помочь мне

1 Ответ

0 голосов
/ 03 марта 2012

Вы можете поставить дополнительные проверки текущей ориентации в viewDidAppear и соответственно изменить вид.Вы всегда можете получить текущую ориентацию устройства как

[[UIDevice currentDevice] orientation];

РЕДАКТИРОВАТЬ: Попробуйте добавить этот метод в свой класс:

-(void)viewDidAppear{
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

 if (deviceOrientation == UIInterfaceOrientationPortrait )
 {
   self.view = self.portraitView;        
 }
 else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation ==    
 UIInterfaceOrientationLandscapeRight)
 {
 self.view = self.landscapeView;    
 }
}

EDIT2- Попробуйте вручную установить ориентацию как:

- (void)viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];
  UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

  [[UIDevice currentDevice] setOrientation:deviceOrientation];
  if (deviceOrientation == UIInterfaceOrientationPortrait )
   {
      self.view = self.portraitView;        
   }
  else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation ==    
 UIInterfaceOrientationLandscapeRight)
   {
      self.view = self.landscapeView;    
   }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...