ios - 使用 AutoLayout 更改 UIImageView 框架大小

标签 ios uiimageview autolayout

我已经使用 AutoLayout 设置了一个 UIImageView。我已经为顶部、底部、前导和尾部设置了约束(我希望宽度和高度根据手机的大小而改变)。在我的属性检查器中,它还显示 ImageView 的宽度和高度。出于某种原因,当 View 首次加载时,顶部空间到顶部布局约束被忽略,图像离开屏幕,使用 IB 中的高度 (332)。添加照片后, View 会按预期调整大小。我 NSLogged ImageView 的框架高度,它正在改变。我不明白,我以为 ImageView 的框架总是一样的。我没有收到任何自动布局错误,一切都是蓝色的。 enter image description here

UIImage *imageToView = [existingImage resizedImageWithBounds:self.selectPhoto.frame.size];//Resize the image for better performance
 self.selectPhoto.contentMode =UIViewContentModeScaleAspectFit;
 NSLog(@"Select Photo Frame Size Height is %f", self.selectPhoto.frame.size.height); //The first time this screen loads, this height is 332.  Every other time (after the user enters a new photo, the height is 310 which is what I want.
self.selectPhoto.image = imageToView;

回顾:为什么我的 self.selectPhoto(屏幕截图中显示的大 ImageView ).frame 发生变化?我仔细检查了 Apple Docs,它没有说明任何关于基于图像大小更改帧的内容。这很重要,因为我正在根据帧大小调整图像大小。

最佳答案

我知道当您不限制宽度和高度时,使用 AutoLayout 时 imageViews 会很有趣。许多图像比 imageView 大得多,因此您可以看到 imageView 变大(可能是因为它无法计算其固有内容大小)。我之前通过限制 imageView 的宽度和高度解决了这个问题,但显然你希望你的宽度和高度随设备大小而变化。

我建议在项目中使用您想要大小的单独 UIView,并将 UIImageView 嵌入其中。为两者设置正确的约束,这应该可以解决您的问题。

关于ios - 使用 AutoLayout 更改 UIImageView 框架大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32542337/

相关文章:

ios - imageView 设置为 UIViewContentModeScaleAspectFill 的 UIButton 不会完全填充小图像

ios - 如何仅针对特定屏幕尺寸缩小字体

iphone - 自定义 UITableViewCell 未显示时始终为 null

ios - 在 UICollectionView 中显示错误的图像

ios - UILabel 行间距

ios - 如何为 UIImageView 设置特定大小

ios - 以编程方式在 UIScrollview 中添加多个 UIImageview

ios - 堆栈 View - 但有 "proportional"间隙

ios - 自动布局在 Xcode 5 上被锁定

ios - 如何让 uitableview 一次加载多个部分?