ios - 如何以编程方式将 UISlider 与 slider 下方的标签连接起来,该 View 通常位于所有 iphone 屏幕上?

标签 ios swift label uislider

我为 UISlider 的范围创建了一个数组,并将每个值放在 slider 下方的标签上,但是如果我更改 iPhone 屏幕(5s、6s、8、Xr) 标签未显示 UISlider 值的正确位置。

我应该如何将 UISlider 数组值的范围与每个 iPhone 屏幕的下面的标签连接起来?

Expected results

Debug View Hierarchy

无论不同的 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/

相关文章:

iphone - 支付交易状态失败

ios - 使用 EventKit 添加事件和日历

swift - 更改 CorePlot 中轴上数字的大小

ios - Swift:打印时崩溃

ios - 如何修复 ionic 2 Apple Mach-O Linker 错误?

ios - 如何在 iOS 中调整各种 iPhone 屏幕尺寸的字体大小?

javascript - 在 nvd3.js 中编辑多条形图中的标签文本

java-me - 在LWUIT j2me中设置标签中文本的大小

ios - 文本 UIButton 周围的边框

iphone - iOS :Get UIImage Orientation inside a UIImageview with exif data?