gpt4 book ai didi

swift - UISlider布局忽略宽度

转载 作者:行者123 更新时间:2023-11-28 06:38:50 28 4
gpt4 key购买 nike

我需要添加一个带有 UILabelUISlider 的弹出 View ,但是我无法设置 UISlider 的宽度.

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

frontPanel = UIView()
frontPanel!.backgroundColor = UIColor.whiteColor()
frontPanel!.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(frontPanel!)

valueSlider = UISlider()
valueSlider!.translatesAutoresizingMaskIntoConstraints = false
valueSlider!.frame = CGRect(x: 0,
y: 0,
width: frontPanel!.frame.width - 16,
height: 35)

sliderLabel = UILabel()
sliderLabel!.translatesAutoresizingMaskIntoConstraints = false

sliderLabel!.text = "Adjust value"

let frontPanelSubviews: [String : AnyObject] = [
"label" : sliderLabel!,
"slider" : valueSlider!
]

frontPanel!.addSubview(sliderLabel!)
frontPanel!.addSubview(valueSlider!)

frontPanel?.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("V:|-[label]-[slider]-|",
options: [],
metrics: nil,
views: frontPanelSubviews)
)
frontPanel?.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("H:|-[label]-|",
options: [],
metrics: nil,
views: frontPanelSubviews)
)
frontPanel?.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("H:|-[slider]-|",
options: [],
metrics: nil,
views: frontPanelSubviews)
)

}

override func viewDidLayoutSubviews() {
frontPanel?.frame = CGRect(x: 0,
y: view.frame.height - 75,
width: view.frame.width,
height: 75)
}

@IBOutlet weak var backPanel: UIView!
@IBOutlet weak var valueLabel: UILabel!

var frontPanel: UIView?
var valueSlider: UISlider?
var sliderLabel: UILabel?
}

在此示例中,UISlider 将始终获得与 UILabel 相同的宽度。

如果我只使用像 H:|-[label]-[slider]-| 这样的水平布局约束,UISlider 会得到 width == 0

如何让 UISlider 使用父 View 的整个宽度(减去每一侧的 8 点空间)?

最佳答案

frontPanel!.translatesAutoresizingMaskIntoConstraints = true

frontPanelViewController.viewsubview 并且您设置了精确的 frame。我的理解是这样的。

关于swift - UISlider布局忽略宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38503746/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com