我为 UISlider 的范围创建了一个数组,并将每个值放在 slider 下方的标签上,但是如果我更改 iPhone 屏幕(5s、6s、8、Xr) 标签未显示 UISlider 值的正确位置。
我应该如何将 UISlider 数组值的范围与每个 iPhone 屏幕的下面的标签连接起来?
无论不同的 iPhone 屏幕,都有一种设置自动布局的方法。你看到的Easy.Layout即EasyPeasy Auto-Layout与Apple文档Auto Layout Constrains相同。
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)
)
}
})
}
最佳答案
您需要使用自动布局在标签上设置约束。这将使其在所有设备上保持在固定位置。
关于ios - 如何以编程方式将 UISlider 与 slider 下方的标签连接起来,该 View 通常位于所有 iphone 屏幕上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56889139/