我在不同的图像视图中使用相同的图像,并具有相同的约束,但是尽管有约束,但最后两幅图像仍不保持宽高比。
这是约束代码。
PS:它们都具有以下2行。 (我只是从每个组中删除了这些行,以避免出现代码壁。)
.contentMode = .scaleAspectFit
.translatesAutoresizingMaskIntoConstraints = false
ImageLineEvents.topAnchor.constraint(equalTo: LabelTitleEvents.bottomAnchor, constant:UI_SPACING_SCROLL).isActive = true
ImageLineEvents.centerXAnchor.constraint(equalTo: ScrollView.centerXAnchor).isActive = true
ImageLineEvents.widthAnchor.constraint(equalTo: VC_UserProfile.view.widthAnchor).isActive = true
ImageLineEvents.widthAnchor.constraint(equalTo: ImageLineEvents.heightAnchor, multiplier: (ImageLineEvents.image?.size.width)! / (ImageLineEvents.image?.size.height)!).isActive = true
ImageLineUpdates.topAnchor.constraint(equalTo: LabelTitleUpdates.bottomAnchor, constant:UI_SPACING_SCROLL).isActive = true
ImageLineUpdates.centerXAnchor.constraint(equalTo: ScrollView.centerXAnchor).isActive = true
ImageLineUpdates.widthAnchor.constraint(equalTo: VC_UserProfile.view.widthAnchor).isActive = true
ImageLineUpdates.widthAnchor.constraint(equalTo: ImageLineUpdates.heightAnchor, multiplier: (ImageLineUpdates.image?.size.width)! / (ImageLineUpdates.image?.size.height)!).isActive = true
ImageLineSoon.topAnchor.constraint(equalTo: LabelTitleSoon.bottomAnchor, constant:UI_SPACING_SCROLL).isActive = true
ImageLineSoon.centerXAnchor.constraint(equalTo: ScrollView.centerXAnchor).isActive = true
ImageLineSoon.widthAnchor.constraint(equalTo: VC_UserProfile.view.widthAnchor).isActive = true
ImageLineSoon.widthAnchor.constraint(equalTo: ImageLineSoon.heightAnchor, multiplier: (ImageLineSoon.image?.size.width)! / (ImageLineSoon.image?.size.height)!).isActive = true
ImageLineToday.topAnchor.constraint(equalTo: LabelTitleToday.bottomAnchor, constant:UI_SPACING_SCROLL).isActive = true
ImageLineToday.centerXAnchor.constraint(equalTo: ScrollView.centerXAnchor).isActive = true
ImageLineToday.widthAnchor.constraint(equalTo: VC_UserProfile.view.widthAnchor).isActive = true
ImageLineToday.widthAnchor.constraint(equalTo: ImageLineToday.heightAnchor, multiplier: (ImageLineToday.image?.size.width)! / (ImageLineToday.image?.size.height)!).isActive = true
在肖像模式下,一切都很好,但是当我转向风景时,最后的两幅图像并不能保持纵横比。我有来自各个方向的日志。
print("ar = \(Float((ImageLineEvents.image?.size.width)!) / Float((ImageLineEvents.image?.size.height)!))")
print("ar = \(Float((ImageLineUpdates.image?.size.width)!) / Float((ImageLineUpdates.image?.size.height)!))")
print("ar = \(Float((ImageLineSoon.image?.size.width)!) / Float((ImageLineSoon.image?.size.height)!))")
print("ar = \(Float((ImageLineToday.image?.size.width)!) / Float((ImageLineToday.image?.size.height)!))")
print("w = \(ImageLineEvents.frame.width)")
print("w = \(ImageLineUpdates.frame.width)")
print("w = \(ImageLineSoon.frame.width)")
print("w = \(ImageLineToday.frame.width)")
print("h = \(ImageLineEvents.frame.height)")
print("h = \(ImageLineUpdates.frame.height)")
print("h = \(ImageLineSoon.frame.height)")
print("h = \(ImageLineToday.frame.height)")
这些来自肖像,一切都如预期。
ar = 162.5
ar = 162.5
ar = 162.5
ar = 162.5
w = 335.0
w = 335.0
w = 335.0
w = 335.0
h = 2.0
h = 2.0
h = 2.0
h = 2.0
但是,如果我转向风景,最后2个会有不同的结果。
ar = 162.5
ar = 162.5
ar = 162.5
ar = 162.5
w = 684.0
w = 684.0
w = 684.0
w = 684.0
h = 4.33333333333333
h = 4.33333333333333
h = 4.0(为什么不是4.33?)
h = 4.0(为什么不是4.33?)
我看了无数遍,但是我仍然不知道这是怎么回事。没有约束错误。从视觉上看,最后两张图像不会遍历所有边缘,因为它们可以保持宽高比,但高度却较低。它们都在ScrollView中,但是我看不出这有多重要。有帮助吗?
编辑: 我添加了视觉效果。前2个约束是第一个图像,后2个约束是最后一个图像。如您所见,它们的宽度在横向上并不相同。所讨论的图像是标题正下方的线,它应从标题传播到点图像尾随。它们在肖像上的表现都不错,但在风景上,最后两个的表现却有所不同。