Skip to content

Instantly share code, notes, and snippets.

@mecid
Last active February 11, 2019 08:33
Show Gist options
  • Save mecid/f3b24a15ee3ef26006d25261f10e352b to your computer and use it in GitHub Desktop.
Save mecid/f3b24a15ee3ef26006d25261f10e352b to your computer and use it in GitHub Desktop.
saccessibility7.swift
import UIKit
class BarChartView: UIView {
private enum Layout {
static let barWidth: CGFloat = 30
static let maxHeightRatio: CGFloat = 0.8
}
var values: [Double] = [] {
didSet {
setNeedsLayout()
}
}
override func layoutSubviews() {
super.layoutSubviews()
guard let max = values.max() else { return }
subviews.forEach { $0.removeFromSuperview() }
var x: CGFloat = Layout.barWidth
let maxHeight: CGFloat = bounds.height * Layout.maxHeightRatio
values.forEach { value in
let height = CGFloat(value / max) * maxHeight
let y = bounds.height - height - safeAreaInsets.bottom
let frame = CGRect(x: x, y: y, width: Layout.barWidth, height: height)
let bar = UIView(frame: frame)
bar.backgroundColor = .gray
addSubview(bar)
x = bar.frame.maxX + Layout.barWidth / 2
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment