0

我正在开发一个 iPad 应用程序,该应用程序以初始屏幕开始,然后移动到登录屏幕。我的应用程序应该支持所有方向,以及 iOS 4.3 及更高版本。为此,我在 plist 中添加了四个方向,并在应用程序委托中添加了以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    [self.window makeKeyAndVisible]; 
    // Override point for customization after application launch
    SplashViewController *aController = [[SplashViewController alloc] initWithNibName:@"SplashView" bundle:nil];
    self.mainViewController = aController;
    [aController release];

    mainViewController.view.frame = [UIScreen mainScreen].bounds;// no effect
    [window setFrame:[[UIScreen mainScreen] bounds]]; //no effect
    //[window addSubview:[mainViewController view]];
    [window setRootViewController:mainViewController];

    return YES;
}

- (NSUInteger)application:(UIApplication *)application     supportedInterfaceOrientationsForWindow:(UIWindow *)window{

    return UIInterfaceOrientationMaskAll;
}

在启动画面中

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

}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
}
- (BOOL)shouldAutorotate{
    return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}
- (void) orientationChanged
{
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            NSLog(@"Portrait");
    else
            NSLog(@"Landscape");

}

在这里,我得到了正确的方向,但是在旋转时,我得到了一个倒置的结果,我得到横向的纵向和纵向的横向。我试图像这样转换代码:

- (void) orientationChanged
{
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            NSLog(@"Landscape");
    else
            NSLog(@"Portrait");

}

我得到第一个结果错误,之后结果是正确的。你能帮我吗?请注意,我已经放置了一些 uielement 进行测试,并且测试结果相同。谢谢

4

1 回答 1

0

经过两天的搜索和测试,我设法解决了这个问题,论坛上的答案都不完整,我是这样解决的:

//this for the orientation
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self orientationChanged];
}

// here is the tricky thing, when startup you should have a special function for the orientation other than the orientationchanged method, and must be called here in viewDidAppear, otherwise it won't work
-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self StartUpOrientation];
}
#pragma mark -
#pragma mark Orientation Methods

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

- (BOOL)shouldAutorotate {
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;

}

- (void) orientationChanged {
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation != UIInterfaceOrientationLandscapeLeft && interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
        [self.view setBounds:CGRectMake(0, 0, 1024, 748)];
        // your code here
    }
    else {
        [self.view setBounds:CGRectMake(0, 0,768,1004)];
        //your code here
    }
}

- (void) StartUpOrientation {
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation != UIInterfaceOrientationLandscapeLeft && interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
        [self.view setBounds:CGRectMake(0, 0,768,1004)];
        // your code here
    }
else {
        [self.view setBounds:CGRectMake(0, 0, 1024, 748)];
        // your code here
    }
}

希望有一天它会帮助某人

于 2013-02-13T17:00:59.670 回答