shouldAutorotateToInterfaceOrientation无法正常工作

时间:2012-11-05 09:30:29

标签: iphone locking orientation mode portrait

如果设备是iPhone,我想将方向锁定为纵向,如果设备是iPad,我想允许所有方向。

我有这段代码,但它没有将iPhone锁定在纵向模式下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
    {
        return NO;

    }
    else
    {
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
        return YES;
    }
    return NO;

}

有什么问题?

2 个答案:

答案 0 :(得分:0)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);

    }
    else{
        return YES;
    }

}

试试这个。希望这会有所帮助。

答案 1 :(得分:0)

if you will navigate the view then force orientation will not work but if you will     present your view then it will work very well and it works for me. In my project all screens are in portrait mode only but only one screen is in landscape mode.   
- (IBAction)startButtonClicked:(id)sender {

CTFailure_RemedyGameViewController *remedyGameController = [[CTFailure_RemedyGameViewController alloc]initWithNibName:@"CTFailure_RemedyGameViewController" bundle:nil];
[self presentModalViewController:remedyGameController animated:NO];
[remedyGameController release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

@end