Skip to content

Commit

Permalink
Merge pull request xmartlabs#1296 from akabab/bug/324-slider-row-layo…
Browse files Browse the repository at this point in the history
…ut-problem

Fix SliderRow Layout problem
  • Loading branch information
Martin Barreto authored Feb 28, 2018
2 parents 7931948 + da62298 commit ad6ea31
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
Binary file modified Example/Media/RowStatics/SliderRow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 28 additions & 15 deletions Source/Rows/SliderRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ open class SliderCell: Cell<Float>, CellType {
open override func setup() {
super.setup()
if !awakeFromNibCalled {
// title
let title = textLabel
textLabel?.translatesAutoresizingMaskIntoConstraints = false
textLabel?.setContentHuggingPriority(UILayoutPriority(rawValue: 500), for: .horizontal)
self.titleLabel = title

let value = detailTextLabel
value?.translatesAutoresizingMaskIntoConstraints = false
value?.setContentHuggingPriority(UILayoutPriority(rawValue: 500), for: .horizontal)
value?.setContentHuggingPriority(UILayoutPriority(500), for: .horizontal)
value?.adjustsFontSizeToFitWidth = true
value?.minimumScaleFactor = 0.5
self.valueLabel = value

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

if shouldShowTitle {
contentView.addSubview(titleLabel)
contentView.addSubview(valueLabel!)
}

if !sliderRow.shouldHideValue {
contentView.addSubview(valueLabel)
}

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

func addConstraints() {
guard !awakeFromNibCalled else { return }
let views: [String : Any] = ["titleLabel": titleLabel, "slider": slider, "valueLabel": valueLabel]
let metrics = ["spacing": 15.0, "valueLabelWidth": 40.0]

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))
}
let title = shouldShowTitle ? "[titleLabel]-spacing-" : ""
let value = !sliderRow.shouldHideValue ? "-[valueLabel(valueLabelWidth)]" : ""

let hContraints = NSLayoutConstraint.constraints(
withVisualFormat: "H:|-\(title)[slider]\(value)-|",
options: .alignAllCenterY,
metrics: metrics,
views: views)

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

contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-[slider]-|", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: metrics, views: views))
contentView.addConstraints(hContraints + vContraints)
}

@objc func valueChanged() {
Expand Down Expand Up @@ -145,6 +157,7 @@ public final class SliderRow: Row<SliderCell>, RowType {
public var minimumValue: Float = 0.0
public var maximumValue: Float = 10.0
public var steps: UInt = 20
public var shouldHideValue = false

required public init(tag: String?) {
super.init(tag: tag)
Expand Down

0 comments on commit ad6ea31

Please sign in to comment.