gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:45:24 25 4
gpt4 key购买 nike

我已经使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com