【问题标题】:Automatically resized imageview自动调整大小的图像视图
【发布时间】:2014-06-28 07:59:39
【问题描述】:

我有一个 scrollView 和 imageView 。 该应用程序在横向和纵向模式下工作。 我使用自动布局。 如果我对 imageview 使用常量。 我加载到 imageviev 中的图像会自动在其大小下展开。

我现在拥有的:

横向模式:

和纵向模式:

我想像这些图片一样

横向模式:

和纵向模式:

如何修复 imageview 中的自动调整大小?

附: iPad 应用程序

谢谢大家的回答!

【问题讨论】:

  • 您是否也在使用 Autolayout 进行纵向?我不清楚为什么您指定在横向模式下使用 Autolayout,而 Autolayout 的整个想法是使您的应用程序屏幕尺寸和方向独立。如果您以编程方式执行此操作,您可以发布一些代码供我们检查吗?
  • 我在界面生成器中的所有方向都使用自动布局

标签: ios objective-c ipad uiscrollview uiimageview


【解决方案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];
}

您应该能够根据需要设置图像的大小...希望对您有所帮助...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 2017-04-29
    • 2013-03-08
    相关资源
    最近更新 更多