我正在尝试这样做:在我家的应用程序中,如果我将设备置于横向模式,我将显示帮助视图。到目前为止它似乎工作正常,但如果我按下按钮去另一个视图,然后我回到家里并将设备置于风景中...我看到一切都是错误的。
以下是两张试图解释问题的图片:
wrong landscape wrong portrait
这是我的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
我希望你能帮助我
答案 0 :(得分:0)
您可以在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;
}
}