我有一个标签栏应用程序。我使用的是XCode 4.3.3。我已经升级到4.5.2与iOS6的东西。
每个视图的shouldAutorotateToInterfaceOrientation
中的代码将检查当前设备方向并正确放置所有UI组件。
但升级到iOS 6后,此代码无法执行。我已经尝试了将近一天来解决这个问题,但没有运气。
我也试过
UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
在viewLoad
, viewDidLoad
, viewWillAppear
如果我想在视图加载期间检查方向,并在视图加载期间将UI组件放置在适当的位置,我该怎么办?请给出你的建议。
谢谢
如果你想检查哪个是当前的方向,那么你只需写在prifix。
#define isPortrait [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown
然后在你想检查方向的地方,在
下面写条件 if(isPortrait)
{
//portrait mode....
}
else
{
//landscape mode....
}
让我知道它是否有效。
编码快乐!
要在ios6中检查方向,你必须在下面的方法和你的信息中写。Plist你必须定义你想要的方向…
让我知道它是否有效!!
编码快乐! !
- (BOOL)shouldAutorotate{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;//you can write here which ever orientation you want..
}
你可以这样管理方向:-
-(BOOL)shouldAutorotate
{
return YES;
}
并每次检查ViewWillApear
设备方向,如:-
- (void)willRotateToOrientation:(UIInterfaceOrientation)newOrientation {
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
if (newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight) {
//set your landscap View Frame
[self supportedInterfaceOrientations];
}
}
else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
if(newOrientation == UIInterfaceOrientationPortrait || newOrientation == UIInterfaceOrientationPortraitUpsideDown){
//set your Potrait View Frame
[self supportedInterfaceOrientations];
}
}
// Handle rotation
}
-(void)viewWillAppear:(BOOL)animated
{
[self willRotateToOrientation:[[UIDevice currentDevice] orientation]];
[super viewWillAppear:YES];
}
可能人们使用检查设备orientation
,就像下面把这行放在ViewWillApear
:-
[[UIApplication sharedApplication] statusBarOrientation];
[[UIDevice currentDevice] orientation];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];
和
-(void)deviceRotated:(NSNotification*)notification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//Do your stuff for landscap
}
else if(orientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
//Do your stuff for potrait
}
}
在viewDidLoad方法中,您需要检查是否相同,因此您需要编写代码,除非它可能会影响视图,如果它不在纵向