我为UISlider的范围创建一个数组,并将每个值放在滑块下方的标签上,但是如果我更改iPhone屏幕(5s,6s,8,Xr), 标签未显示UISlider值的正确位置。
在每个iPhone屏幕上,如何将UISlider数组值的范围与下面的Label连接起来?
有一种方法可以设置自动布局,而不管iPhone屏幕如何。在其中看到Easy.Layout的EasyPeasy Auto-Layout与Apple文档“自动布局约束”相同。
func setupLabels() {
let values = [0, 3.7, 11, 22, 43, 50, 100]
values.forEach({
let sliderValuesLabel = UILabel()
if $0 == values.first {
sliderValuesLabel.text = "ALLE"
wrapperView.addSubview(sliderValuesLabel)
sliderValuesLabel.easy.layout(
Top(15).to(uislider),
Leading().to(uislider, .leading), // Left Position
Bottom(1)
)
} else if $0 == values.last {
sliderValuesLabel.text = "\(Int($0))+"
wrapperView.addSubview(sliderValuesLabel)
sliderValuesLabel.easy.layout(
Trailing().to(uislider, .trailing), // Right Position
Top(15).to(uislider),
Bottom(1)
)
} else {
sliderValuesLabel.text = "\(Int($0))"
wrapperView.addSubview(sliderValuesLabel)
let thumbRect = uislider.thumbRect(forBounds: uislider.bounds,
trackRect: uislider.trackRect(forBounds: uislider.bounds),
value: uislider.value)
let thumRectToView = uislider.convert(thumbRect, to: uislider ) // Convert
sliderValuesLabel.easy.layout(
Top(15).to(uislider),
Bottom(1),
CenterX(thumRectToView.midX)
)
}
})
}
答案 0 :(得分:0)
您将需要使用“自动布局”在标签上设置约束。这样会将其固定在所有设备上。