【问题标题】:How to properly add an UIImageView inside an UIStackView如何在 UIStackView 中正确添加 UIImageView
【发布时间】:2015-11-08 17:39:58
【问题描述】:

我的 UIImageView 加载了一张高分辨率的图片。当我将 UIImageView 添加到 UIStackView 时,堆栈视图将增长到 1900x1200 尺寸。 UIImageView contentMode 设置为 Aspect Fill。

如何使图像在添加到堆栈视图后保持其当前尺寸 (130x130)?

【问题讨论】:

  • 添加约束。你的问题太笼统了……
  • [self addConstraint:[NSLayoutConstraint constraintWithItem:myUIImageVIew attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.f constant:130]]; [self addConstraint:[NSLayoutConstraint constraintWithItem:myUIImageVIew 属性:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqual toItem:nil 属性:NSLayoutAttributeNotAnAttribute 乘数:1.f 常量:130]];

标签: ios uiimageview autolayout uistackview


【解决方案1】:

我希望你现在已经回答了你的问题,但如果没有,你就去吧:

在将 UIImageView 放入堆栈视图之前,只需为其添加高度和宽度约束即可。将它们都设为 130,您应该一切顺利。

【讨论】:

  • 您好,我想知道为什么我们必须在 UIIMageView 中添加约束,然后才能放入堆栈视图。 stackView 不应该为我们处理约束吗?
  • 它有助于定位约束,并且做得很好。但是,如果堆栈视图中的所有内容都必须具有相同的高度和宽度,那将是一个非常严格的结构。它可以自动为您设置高度和宽度,但如果您想要更好的控制,高度和宽度限制是要走的路。
  • 如此简单,太棒了。我在 stackView 中与 UIImageView 苦苦挣扎,在实际将其放入堆栈视图之前没有考虑对其的约束。谢谢!
【解决方案2】:

我可以通过设置图像视图的纵横比来克服这个问题。我的UIImageView 没有直接添加到UIStackView,而是用普通的UIView 包裹。这样我可以避免直接干扰UIStackView 为每个添加的子视图创建的任何约束。

使用 PureLayout 的示例:

#import <math.h>
#import <float.h>

@interface StackImageView : UIView

@property (nonatomic) UIImageView *imageView;
@property (nonatomic) NSLayoutConstraint *aspectFitConstraint;

@end

@implementation StackImageView

// skip initialization for sanity
// - (instancetype)initWithFrame:...

- (void)setup {
    self.imageView = [[UIImageView alloc] initForAutoLayout];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;

    [self addSubview:self.imageView];

    // pin image view to superview edges
    [self.imageView autoPinEdgesToSuperviewEdges];
}

- (void)setImage:(UIImage *)image {
    CGSize size = image.size;
    CGFloat aspectRatio = 0;

    // update image
    self.imageView.image = image;

    if(fabs(size.height) >= FLT_EPSILON) {
        aspectRatio = size.width / size.height;
    }

    // Remove previously set constraint
    if(self.aspectFitConstraint) {
        [self.imageView removeConstraint:self.aspectFitConstraint];
        self.aspectFitConstraint = nil;
    }

    // Using PureLayout library
    // you may achieve the same using NSLayoutConstraint
    // by setting width-to-height constraint with
    // calculated aspect ratio as multiplier value
    self.aspectFitConstraint =
    [self.imageView autoMatchDimension:ALDimensionWidth  
                           toDimension:ALDimensionHeight 
                                ofView:self.imageView
                        withMultiplier:aspectRatio 
                              relation:NSLayoutRelationEqual];
}

@end

【讨论】:

    【解决方案3】:

    设置

    clipToBounds = true
    

    您可以通过界面生成器通过检查图像视图选项或 您可以添加代码为

    imageView.clipToBounds = true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多