2010-04-29 4 views
12

Salut à tous. J'ai une question assez simple. Je développe une application iPad "riche", et j'ai deux images de fond spécifiquement conçues pour le paysage et le portrait. Je voudrais que ImageView change automatiquement en fonction de l'orientation des périphériques. (comme à peu près toutes les applications iPad Apples).Modification de UIView sur le changement d'orientation

Quelqu'un peut-il me diriger dans la bonne direction? Je suppose que ce serait quelque chose que je ferais sur viewDidLoad ..

+0

répétition possible de http://stackoverflow.com/questions/2489845/rotate-uiviewcontroller-to-counteract-changes-in-uiinterfaceorientation/2490719#2490719 –

Répondre

22

La meilleure chose que vous pouvez faire est de changer les cadres de vos cadres subview en fonction de vos orientations d'interface. Vous pouvez le faire comme:

#pragma mark - 
#pragma mark InterfaceOrientationMethods 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return (UIInterfaceOrientationIsPortrait(interfaceOrientation) || UIInterfaceOrientationIsLandscape(interfaceOrientation)); 
} 

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ 
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 
    if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){ 
     //self.view = portraitView; 
     [self changeTheViewToPortrait:YES andDuration:duration]; 

    } 
    else if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){ 
     //self.view = landscapeView; 
     [self changeTheViewToPortrait:NO andDuration:duration]; 
    } 
} 

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{ 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:duration]; 

    if(portrait){ 
     //change the view and subview frames for the portrait view 
    } 
    else{ 
     //change the view and subview frames for the landscape view 
    } 

    [UIView commitAnimations]; 
} 

Espérons que cela aide.

+0

qui a fait l'affaire, merci! – Designeveloper

+0

Les macros 'UIInterfaceOrientationIsPortrait()' et 'UIInterfaceOrientationIsLandscape()' améliorent vraiment la lisibilité pour ces types de situations ... mais, une excellente solution, néanmoins! – Nate

+0

@Nate Merci pour la suggestion .. Changé la réponse. :) –

9

J'ai effectivement trouvé une alternative très simple autour de cela. Depuis que je suis en train de changer l'image juste en arrière-plan, en ajoutant ce ..

`

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration { 
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == 
     UIInterfaceOrientationPortraitUpsideDown) { 
     [brownBackground setImage:[UIImage imageNamed:@"Portrait_Background.png"]]; 
    } else { 
     [brownBackground setImage:[UIImage imageNamed:@"Landscape_Background.png"]]; 
    } 
} 

`

Modifie l'arrière-plan d'une UIImageView déclarée fondée sur l'orientation. Seul inconvénient est, l'image d'arrière-plan en cours n'est pas visible dans le constructeur de l'interface car il est manipulé avec du code.

7

Une petite addition à l'approche de Madhup, ce qui est génial. J'ai trouvé que je devais ajouter à viewDidLoad pour définir l'image d'arrière-plan initial portrait ou paysage:

// set background image 
if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"portraitBG.png"]]; 
} else { 
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"landscapeBG.png"]]; 
} 

merci encore Madhup

+0

c'est la manière la plus correcte –

+0

+1 pour 'self.interfaceOrientation' –

0

Vous pouvez résumer cela tout à fait dans votre UIView en regardant si bounds.width > bounds.height

Cette peut être souhaitable si vous écrivez un petit contrôle conscient de soi.

class MyView: UIView { 
    override func layoutSubviews() { 
    super.layoutSubviews() 
    if bounds.height > bounds.width { 
     println("PORTRAIT. some bounds-impacting event happened") 
    } else { 
     println("LANDSCAPE") 
    } 
    } 
} 
Questions connexes