2016-04-22 8 views
5

È un nome di file della superclasse Apporientationviewcontroller.h/m. E sto chiamando questa super classe in tutte le altre sottoclassi. In modo che se "isPortraitModeONN" è "ON" allora tutto lo schermo dovrebbe funzionare solo in modalità verticale. se l'utente tenta di cambiare il dispositivo in orizzontale, non dovrebbe ruotare. dovrebbe essere sempre in modalità verticale se l'interruttore è "ON" .. Nel mio caso mentre ridi l'app è in modalità verticale. ma se ruoto lo schermo sta cambiando verso il paesaggio. ma non dovrebbe cambiare il suo orientamento fino a trasformare OFF l'interruttore nelle impostazioni ..Come forzare l'orientamento di tutto lo schermo in modalità verticale se l'interruttore è attivo ?? se l'utente ruota non dovrebbe cambiare il suo orientamento

if(isPortraitModeONN) 
    { 
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; 
    } 
    else 
    { 
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft){ 
     [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; 
    } 
    else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) 
    { 
     [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft]; 
    } 
    else{ 
     [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; 
    } 
} 

risposta

3

Questo codice funziona per me un po '..

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator: (id<UIViewControllerTransitionCoordinator>)coordinator 
{ 
///if isPortraitMode On then force the orientation to portrait if it's other than Portrait 
///else force the orientation to landscape 

if (isPortraitModeONN) 
{ 
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) 
    { 
     // do whatever 
     UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
     if (UIInterfaceOrientationIsLandscape(orientation)) { 
      ///App is rotating to landscape, force it to portrait 
      NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; 
      [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 
     } 
    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) 
    { 

    }]; 
} 
else{ 

    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) 
    { 
     // do whatever 
     UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
     if (UIInterfaceOrientationIsPortrait(orientation)) { 
      ///App is rotating to portrait, force it to landscape 
      NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight]; 
      [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 
     } 
    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) 
    { 
    }]; 
    } 
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; 
} 
0

Goto Target -> Informazioni Distribuzione -> dispositivo di impostare l'orientamento al ritratto solo

enter image description here

Come il par la tua domanda se vuoi farlo toccando il pulsante Prova questo codice non sono sicuro che funzioni sul tuo progetto ma funziona per il mio progetto fittizio per favore provalo

[[UIDevice currentDevice] setValue: 
         [NSNumber numberWithInteger: UIInterfaceOrientationPortrait] 
          forKey:@"orientation"]; 

variazione dell'orientamento dispositivo per entrambi i pulsanti

+0

sta funzionando o no? –

+0

Grazie per il tuo commento..se seguo questa procedura, l'app funzionerà solo in modalità verticale. Ma l'app dovrebbe supportare anche l'orientamento orizzontale. Se l'interruttore verticale è "OFF", dovrebbe funzionare solo in modalità orizzontale. Il mio processo è (i) se Portrait Switch è "ON", quindi l'app dovrebbe funzionare in modalità verticale solo se l'utente ruota lo schermo non dovrebbe cambiarne l'orientamento. (ii) se l'interruttore verticale è impostato su "OFF", l'app dovrebbe funzionare in modalità orizzontale solo se l'utente ruota lo schermo non dovrebbe modificarne l'orientamento. – Nithya

+0

si prega di verificare la risposta aggiornata –

0

codice Si dovrebbe essere così,

AppDelegate.h

@property() BOOL restrictRotation; 
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window; 

AppDelegate.m

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    if(self.restrictRotation) 
    return UIInterfaceOrientationMaskLandscape; //you can change upsidedown etc 
else 
    return UIInterfaceOrientationMaskPortrait; // you can change landscape right or left only 

//this should be your initial app orientation because by default value of bool is false 
} 

SettingsViewController (da dove vuoi cambiare intere orientamenti app)

-(void) restrictRotation:(BOOL) restriction 
{ 
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
    appDelegate.restrictRotation = restriction; 
    [appDelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window]; 
} 

non dimenticate di importare appdelegate.h;)

E infine dal switch per cambiare l'orientamento,

[self restrictRotation:YES]; // or NO 

È possibile manipolare supportedInterfaceOrientationsForWindow secondo il requisito come è possibile impostare la proprietà come int invece di bool e può impostare più orientamenti per un diverso valore int.

speranza che questo vi aiuterà :)

0

potete fare come questo-

  1. Add - @property() BOOL isPortraitModeONN; in AppDelegate.hclass.
  2. Ora codificare ciò in AppDelegate.m -

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    
        self.isPortraitModeONN=YES; 
    
        return YES; 
    } 
    
    -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
        { 
    
         if(self.isPortraitModeONN) 
          return UIInterfaceOrientationMaskPortrait; 
         else 
          return UIInterfaceOrientationMaskLandscape; 
    
        } 
    
  3. Ora Aggiungere IBAction di UIButton per cambiare l'orientamento in Apporientationviewcontroller.m -

    - (IBAction)changeOrientation:(id)sender { 
    
        if (allowedToRotate) { 
    
         allowedToRotate=NO; 
    
         AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
    
         appDelegate.isPortraitModeONN = NO; 
    
         NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]; 
    
         [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 
    
         }else{ 
    
         allowedToRotate=YES; 
    
         AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
    
         appDelegate.isPortraitModeONN = YES; 
    
         NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; 
    
         [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 
    
        } 
        } 
    

definire in Apporientationviewcontroller.mBOOL allowedToRotate; valore di default è YES;

Problemi correlati