문제

I have a scrollView and imageView in it. The app works in landscape and portrait mode. And i use auto layout. If i use constant for imageview. images that I loaded into imageviev it automatically expands under its size.

What i have now:

Landscape mode: enter image description here

and Portrait mode: enter image description here

I want to make it as in these pictures

Landscape mode:

and Portrait mode: enter image description here

How do I fix the auto resize in imageview?

P.S. App for iPad

Thank you all for answers!

도움이 되었습니까?

해결책

Here is an example of code based Autolayout that should help you

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

You should be able to set the size of the images as you require... I hope it helps...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top