2014-05-10 23 views
0

我有一個scrollView和imageView在裏面。 該應用程序在橫向和縱向模式下工作。 而我使用自動佈局。 如果我使用常量的imageview。 我加載到imageviev的圖像會自動在其大小下展開。自動調整大小的圖像查看

我現在擁有的一切:

風景模式: enter image description here

和人像模式: enter image description here

我想讓它在這些照片

風景模式:

和肖像模式: enter image description here

如何修復imageview中的自動調整大小?

P.S. App for iPad

謝謝大家的回答!

+0

你是否也使用Autolayout進行人像拍攝?我不清楚爲什麼你指定你在橫向模式下使用Autolayout,當Autolayout的整個想法是讓你的應用屏幕大小和方向獨立。如果你正在編程,你可以發佈一些代碼供我們檢查嗎? – eharo2

+0

我在界面生成器中使用Autolayout進行所有方向 – DimonDeveloper

回答

1

這裏是基於自動佈局的代碼的例子,應該幫助你

#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]; 
} 

您應該能夠根據您的需要...我希望它有助於設置圖像的大小...