2016-06-25 2 views
-1

j'ai un photoBroser qui est héritée uiview:iOS: Comment laisser un `portrait de soutien uiview` et l'orientation paysage

@interface SDPhotoBrowser : UIView <UIScrollViewDelegate> 

Mais maintenant j'ai une obligation de laisser le photoBroser soutien iphone portrait et paysage orientation, et comment le faire?

my photobroser

EDIT: Dans ma demande, mon application ne supporte que l'orientation portrait, que dans mon photoBrowser qui est uiview, je veux soutenir l'orientation portrait and landscape.

EDIT: Je demande la réponse de J.Hunter, et sa réponse est très bonne, dans le codage en fait, je suggère une fonction pour changer portrait and landscape orientation .Lorsque je rejette mon photoBrowser, il arrive un problème, après mon photoBrowser destituer, la appareil est landscape orientation encore, et donc j'utilise le code ci-dessous pour laisser mon appareil portrait orientation, puis mon photoBrowser rejeter, donc montrer raisonnablement.

//(force landscape:[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];) 
//(force Portrait:[self interfaceOrientation:UIInterfaceOrientationPortrait];) 
+ (void)interfaceOrientation:(UIInterfaceOrientation)orientation 
{ 
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { 
     SEL selector = NSSelectorFromString(@"setOrientation:"); 
     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]]; 
     [invocation setSelector:selector]; 
     [invocation setTarget:[UIDevice currentDevice]]; 
     int val = orientation; 
     // from 2 
     [invocation setArgument:&val atIndex:2]; 
     [invocation invoke]; 
    } 
} 

Cette fonction peut être utile pour un code support portrait and landscape orientation parfait.

+1

Est-ce votre soutien d'applications à la fois portrait et orientation paysage? Ou seulement soutenir l'orientation portrait? –

+0

Dans Xcode à la racine du projet, vous avez Général> Infos sur le déploiement> Orientation du périphérique ....., puis vérifiez l'orientation souhaitée. –

+0

@ J.Hunter Je m'excuse pour ma question partielle, et j'ai complété quelques description à ma question. – lme

Répondre

1

Maintenant, je sais que votre application prend uniquement en charge l'orientation portrait. Il vous faudra deux étapes pour prendre en charge l'orientation portrait et paysage dans un contrôleur de vue spécial.
Je suppose que vous en utilisant Objective-C

1. ajouter une propriété à AppDelegate.h nommé @property (nonatomic, assign) BOOL shouldRotation;
2. Dans 'AppDelegate.m', mettre en œuvre la méthode d'orientation de UIApplicationDelegate-- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window, tout comme

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { 
    if (self.shouldRotation) { 
     return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape; 
    } 
    else { 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 

Vous pouvez maintenant prendre en charge l'orientation paysage dans n'importe quel UIViewController souhaité.

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // enable landscape orientation 
    [UIApplication sharedApplication].delegate.shouldRoration = YES; 
} 

- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated]; 

    // disable landscape orientation 
    [UIApplication sharedApplication].delegate.shouldRoration = NO; 
} 

Peut-être utile.
PS, il y a une autre façon de le résoudre:

  1. support application Vous toute l'orientation
  2. Toutes vos UIViewController override - (UIInterfaceOrientationMask)supportedInterfaceOrientations, retour UIInterfaceOrientationMaskPortrait ou UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape
+0

Votre réponse est très corrects pour moi.Merci. – lme