2013-01-04 5 views
5

Nella mia app universale ho bisogno di gestire diversi orientamenti per iphone e ipad. Per ipad ho bisogno di consentire l'orientamento orizzontale e solo per il ritratto di iphone. Sono tornato qui di seguito il codice in un primo momentoProblema di orientamento IOS 6 nell'app universale

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
     return UIInterfaceOrientationIsPortrait(interfaceOrientation); 

    return UIInterfaceOrientationIsLandscape(interfaceOrientation); 
} 

sta lavorando bene in IOS 5, ma in IOS 6 metodo autorotate non è affatto licenziato. Dopo che ho cambiato il metodo per,

-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskAll; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

anche questo metodi non sono affatto sparati in IOS 6.

La mia impostazione plist è

enter image description here

ho bisogno di gestire sia orientamento [iPhone-portrait, iPad-landscape] per entrambi IOS 5 e IOS 6. Per favore guidami a risolvere questo problema.

+0

vedere Quindi la mia risposta http: // StackOverflow.it/questions/12933089/i-want-to-make-my-appliaction-only-in-landscape-orientation-in-ios-both-ios-5-a – Deepesh

+0

In aggiunta alla risposta accettata, 'preferredInterfaceOrientationForPresentation' chiede un singolo orientamento e stai restituendo una maschera ('UIInterfaceOrientationMaskPortrait') invece di es. 'UIInterfaceOrientationPortrait'. – Jesper

risposta

3

È rootviewcontroller un controller UINavigation o UITabbarcontroller ?? Se è così, questi metodi non funzioneranno se si stanno chiamando questi metodi nel proprio controller di visualizzazione.

Quindi creare una categoria di obiettivi C su questi controller di vista contenitore e aggiungere al progetto.

@implementation UINavigationController (autorotate) 


-(NSUInteger)supportedInterfaceOrientations 
{ 
    //make the check for iphone/ipad here 

if(IPHONE) 
    { 
return UIInterfaceOrientationMaskPortrait; 
    } 
else 
    { 
return UIInterfaceOrientationMaskLandscape; 
    } 

} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
return UIInterfaceOrientationPortrait; 
} 

- (BOOL)shouldAutorotate 
{ 
return NO; 
} 
0

quello che vuoi è il seguente:

#pragma mark - iOS 5.1 and under Rotation Methods 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; { 
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){ 
    return UIInterfaceOrientationIsPortrait(interfaceOrientation); 
    } 
    else{ 
    return UIInterfaceOrientationIsLandscape(interfaceOrientation); 
    } 
} 

#pragma mark - iOS 6.0 and up Rotation Methods 

- (BOOL)shouldAutorotate; { 
    return YES; 
} 

- (NSUInteger)supportedInterfaceOrientations; { 
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){ 
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown); 
    } 
    else{ 
    return UIInterfaceOrientationMaskLandscape; 
    } 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation; { 
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){ 
    return UIInterfaceOrientationPortrait; 
    } 
    else{ 
    return UIInterfaceOrientationLandscapeLeft; 
    } 
} 

Quello che devi fare in modo di è 2 volte.

Primo, che questo è implementato in voi RootViewController. Se si incorpora il proprio UIViewController all'interno di UINavigationController (o UITabBarController), è necessario creare una classe personalizzata UINavigationController (o personalizzata UITabBarController) che implementa questi metodi e la usa.

Secondo, è necessario assicurarsi che gli orientamenti siano supportati nel file Info.plist. In caso contrario, il sistema sovrascriverà i valori restituiti da questi metodi (Nota: è possibile modificare facilmente questi valori facendo clic anche sui pulsanti nella pagina Riepilogo obiettivi.

Spero che aiuti!

2

aggiungere questo metodo per la vostra applicazione delegare il suo lavoro

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window // iOS 6 autorotation fix 
{ 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
     return UIInterfaceOrientationPortraitUpsideDown; 
    } 
    else 
     return UIInterfaceOrientationMaskAll; 
} 
+0

semplice e funziona perfettamente! –

0

100% Ecco la versione rapida di risposta di Pradeep. Non è necessario creare una sottoclasse tutti i controller di navigazione per ottenere ciò che si vuole lavorare (aka, spegnere orientamento orizzontale per iPhone solo)

Basta aggiungere questo per la vostra applicazione delegato:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int { 
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone { 
     return Int(UIInterfaceOrientationMask.Portrait.rawValue) 
    } else { 
     return Int(UIInterfaceOrientationMask.All.rawValue) 
    } 
}