[[UIApplication sharedApplication] statusBarOrientation]; дает неправильное значение при первом вызове

Я работаю над приложением для iPad, которое начинается с экрана-заставки, а затем переходит к экрану входа в систему. Мое приложение должно поддерживать всю ориентацию, а также iOS 4.3 и новее. Для этого я добавил четыре ориентации в список и следующий код в делегате приложения:

- (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, и тест дал тот же результат. Спасибо


person Firas KADHUM    schedule 12.02.2013    source источник


Ответы (1)


Мне удалось решить этот вопрос после двух дней поиска и тестирования, ни один из ответов на форуме не был полным, и вот как я решил:

//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
    }
}

надеюсь, что это когда-нибудь поможет кому-то

person Firas KADHUM    schedule 13.02.2013
comment
но все та же проблема при переходе от одного представления к другому - person Firas KADHUM; 14.02.2013