【发布时间】:2018-01-06 17:15:01
【问题描述】:
我尝试以编程方式在进度条图像背景上方添加用作进度条的图像。
但它在 iPhone 8 和 iPhone 8+ 上的呈现方式不同:
使用的代码如下:
private func updateGauge() {
// Progress value to apply to not overedge content view
let applicableProgress = 1.0
// Get proper image
self.image = getImageColorFrom(progress: applicableProgress)
// Progress bar frame
if let superviewHeight = self.superview?.frame.size.height {
// Height
let progressViewHeight = CGFloat(applicableProgress) * (superviewHeight-borderVertical*2)
// Width
let progressViewWidth = self.superview!.frame.size.width - (borderHorizontal*2)
// Build y position
let yPos = superviewHeight - progressViewHeight
// Finally set frame
self.frame = CGRect(x: borderHorizontal-decalageHorizontal, y: yPos-borderVertical+decalageVertical, width: progressViewWidth, height: progressViewHeight)
}
}
private func getImageColorFrom(progress: Double) -> UIImage {
let image: UIImage
// Retrieve the good background color based on progress value
if progress == 0 {
image = UIImage()
}
else if progress < greenMaxValue {
image = UIImage(progressBar: ProgressBarColor.Green)// "jaugeverte.png"
}
else if progress < orangeMaxValue {
image = UIImage(progressBar: ProgressBarColor.Orange)// "jaugeorange.png"
}
else {
image = UIImage(progressBar: ProgressBarColor.Red)// "jaugerouge.png"
}
// Build resizable image
return image.resizableImage(withCapInsets: UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0), resizingMode: .stretch)
}
关于信息,两个图像都用作 IBOutlet,这就是我们在代码中没有 addSubview() 调用的原因。 我记录了帧大小,结果如下:
progressViewBackgroundFrame (8 / 8+): (17.0, 67.0, 45.0, 460.0) / (17.0, 71.0, 45.0, 525.0)
progressViewFrame (8 / 8+): (8.0, 9.0, 27.0, 443.0) / (8.0, 9.0, 27.0, 508.0)
为什么图像在 iPhone 8 和 8+ 上呈现不同?图像帧是预期的,是 resizableImage 函数的问题吗?
提前致谢, 问候。
【问题讨论】:
-
您将调整大小模式设置为拉伸.. 纵横比缩放怎么样?
-
怎么做? resizingMode 的唯一可用选项是 .stretch 和 .tile(已经尝试过最后一个,但不是预期的)。
-
感谢您的帮助,但最终还是图片本身的问题(见下文)。
标签: ios swift image image-resizing resizable