我正在使用一个选择器来旋转通过基本的颜色条的应用程序。这些条应该与高度匹配,并且位于背景图像宽度的特定位置。该图像是一个可缩放的矢量,并设置为Aspect Fit。该图像还包含一些空白区域,因此不能简单地将它们设置为与图像完全相同的高度。要创建酒吧,我创建了一个简单的长方形UIView
并根据需要设置背景颜色。拾取器方法如下:UIView高度不能正确缩放与跨设备图像
imageSize = AVMakeRectWithAspectRatioInsideRect(myImage.image!.size, myImage.frame)
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
let imageHeight = self.imageSize.height
let imageWidth = self.imageSize.width
let barWidth = imageWidth * 0.055
var frame: CGRect
switch component {
case 1: // First component needs to match tan portions height
let barHeight = imageHeight * 0.85
frame = CGRect(x: -barWidth/2, y: -barHeight/2, width: barWidth, height: barHeight)
break
case 3, 4, 5: // another part of image has different height
let barHeight = imageHeight * 0.64
frame = CGRect(x: -barWidth/2, y: -barHeight/2, width: barWidth, height: barHeight)
break
default: // return empty views for spacers
return UIView()
}
let stripe = UIView(frame: frame)
stripe.backgroundColor = getColor(component, row: row)
return stripe
}
要计算imageHeight
我用过多种方法,都没有成功。我直接使用了myImage.image.size.height
以及使用AVMakeRectWithAspectRatioInsideRect
。图像跨越设备的宽度。以下是图像的条子的一些截图:
上6S:
上6S加:
正如您所看到的,该条不与背景图像的淡褐色部分正确对齐,而且水平位置也会关闭。我曾认为使用百分比可以通过缩放来保持适当的对齐,但我想我错了。
有没有人有这方面的经验,可能能够看到我做错了什么?
我不确定你应该使用'let barHeight = imageHeight * 0.85',如果你想要它等于图像高度。您应该使用大小类以编程方式查看。 –
为什么不使用自动布局?你在哪里得到0.85的计算? 如果你想这样做,请'let barHeight = screenHeight * 0.85' –
@RyanCollins我不希望它完全等于图像高度。图像有一些我不需要匹配的空白区域。 –