2014-05-10 7 views
0

J'ai un scrollView et imageView dedans. L'application fonctionne en mode paysage et portrait. Et j'utilise la mise en page automatique. Si j'utilise constant pour l'imageview. images que j'ai chargé dans imageviev il se développe automatiquement sous sa taille.Redimensionnement automatique de l'image

Ce que j'ai maintenant:

mode paysage: enter image description here

et le mode Portrait: enter image description here

Je veux faire comme dans ces images

mode paysage:

et le mode portrait: enter image description here

Comment est-ce que je fixe le redimensionnement automatique en imageview?

P.S. App pour iPad

Merci à tous pour vos réponses!

+0

Utilisez-vous aussi Autolayout pour le portrait? Il n'est pas clair pour moi pourquoi vous spécifiez que vous utilisez Autolayout en mode paysage, lorsque l'idée d'Autolayout est de rendre la taille et l'orientation de votre écran d'application indépendantes. Si vous le faites par programme, pouvez-vous poster du code pour que nous puissions vérifier? – eharo2

+0

J'utilise Autolayout pour toutes les orientations dans le constructeur d'interfaces – DimonDeveloper

Répondre

1

Voici un exemple de code basé Autolayout qui devrait vous aider

#import "UniversalViewController.h" 

@interface UniversalViewController() 
@property (strong, nonatomic) UIImageView *myImageView; 
@end 

@implementation UniversalViewController 
@synthesize myImageView; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.view.backgroundColor = [UIColor blackColor]; 

    UIImage *myImage = [UIImage imageNamed:@"Velvet_Underground_and_Nico.jpg"]; 
    myImageView = [[UIImageView alloc] initWithImage:myImage]; 
    [myImageView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    [self.view addSubview:myImageView]; 
    [self setWidth:300 andHeight:300 toView:myImageView]; 
    [self centerView1:myImageView toView2:self.view]; 
} 

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    [self.view removeConstraints:self.view.constraints]; 
    [self centerView1:myImageView toView2:self.view]; 
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
     [self setWidth:450 andHeight:250 toView:myImageView]; 
    } else { 
     [self setWidth:300 andHeight:300 toView:myImageView]; 
    } 
} 

#pragma mark custom Autolayout methods 
- (void) setWidth:(float)width andHeight:(float)height toView:(UIView *)view { 

    NSLayoutConstraint *myConstraint; 
    myConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:width]; 
    [self.view addConstraint:myConstraint]; 

    myConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:height]; 
    [self.view addConstraint:myConstraint]; 
} 

- (void) centerView1:(UIView *)view1 toView2:(UIView *)view2 { 
    NSLayoutConstraint *myConstraint; 
    myConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]; 
    [self.view addConstraint:myConstraint]; 

    myConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]; 
    [self.view addConstraint:myConstraint]; 
} 

Vous devriez être en mesure de définir la taille des images que vous avez besoin ... J'espère que ça aide ...

Questions connexes