Last active
February 11, 2019 08:33
-
-
Save mecid/f3b24a15ee3ef26006d25261f10e352b to your computer and use it in GitHub Desktop.
saccessibility7.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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