Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SliderRow Layout problem #1296

Merged
merged 5 commits into from
Feb 28, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix SliderRow contraints (+ display valueLabel in any case, even if n…
…o title provided)
  • Loading branch information
akabab committed Oct 24, 2017
commit d32fc0ecf68a1dcf87183fa890f7b4687d2a790e
36 changes: 20 additions & 16 deletions Source/Rows/SliderRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ open class SliderCell: Cell<Float>, CellType {
open override func setup() {
super.setup()
if !awakeFromNibCalled {
// title
let title = textLabel
textLabel?.translatesAutoresizingMaskIntoConstraints = false
textLabel?.setContentHuggingPriority(UILayoutPriority(500), for: .horizontal)
Expand All @@ -70,6 +69,8 @@ open class SliderCell: Cell<Float>, CellType {
let value = detailTextLabel
value?.translatesAutoresizingMaskIntoConstraints = false
value?.setContentHuggingPriority(UILayoutPriority(500), for: .horizontal)
value?.adjustsFontSizeToFitWidth = true
value?.minimumScaleFactor = 0.5
self.valueLabel = value

let slider = UISlider()
Expand All @@ -79,8 +80,8 @@ open class SliderCell: Cell<Float>, CellType {

if shouldShowTitle {
contentView.addSubview(titleLabel)
contentView.addSubview(valueLabel!)
}
contentView.addSubview(valueLabel!)
contentView.addSubview(slider)
addConstraints()
}
Expand All @@ -94,25 +95,28 @@ open class SliderCell: Cell<Float>, CellType {
super.update()
titleLabel.text = row.title
valueLabel.text = row.displayValueFor?(row.value)
valueLabel.isHidden = !shouldShowTitle && !awakeFromNibCalled
titleLabel.isHidden = valueLabel.isHidden
titleLabel.isHidden = !shouldShowTitle
slider.value = row.value ?? 0.0
slider.isEnabled = !row.isDisabled
}

func addConstraints() {
guard !awakeFromNibCalled else { return }

let views: [String : Any] = ["titleLabel": titleLabel, "valueLabel": valueLabel, "slider": slider]
let metrics = ["vPadding": 12.0, "spacing": 12.0]
if shouldShowTitle {
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-[titleLabel]-[valueLabel]-|", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: metrics, views: views))
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-vPadding-[titleLabel]-spacing-[slider]-vPadding-|", options: NSLayoutFormatOptions.alignAllLeft, metrics: metrics, views: views))
} else {
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-vPadding-[slider]-vPadding-|", options: NSLayoutFormatOptions.alignAllLeft, metrics: metrics, views: views))
}

contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-[slider]-|", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: metrics, views: views))
let views: [String : Any] = ["titleLabel": titleLabel, "slider": slider, "valueLabel": valueLabel]
let metrics = ["spacing": 15.0, "valueLabelWidth": 40.0]

let hContraints = NSLayoutConstraint.constraints(
withVisualFormat: "H:|-\(shouldShowTitle ? "[titleLabel]-spacing-" : "")[slider]-[valueLabel(valueLabelWidth)]-|",
options: .alignAllCenterY,
metrics: metrics,
views: views)

let vContraints = NSLayoutConstraint.constraints(
withVisualFormat: "V:|-[slider]-|",
options: .alignAllCenterX,
metrics: metrics,
views: views)

contentView.addConstraints(hContraints + vContraints)
}

@objc func valueChanged() {
Expand Down