Nicer toolbar buttons
This commit is contained in:
57
App/Common UI/SegmentedReliefButton.swift
Normal file
57
App/Common UI/SegmentedReliefButton.swift
Normal file
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// SegmentedReliefButton.swift
|
||||
// App
|
||||
//
|
||||
// Created by James Magahern on 8/14/20.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
class SegmentedReliefButton: ReliefButton
|
||||
{
|
||||
var children: [ReliefButton] = [] {
|
||||
willSet { children.forEach { $0.removeFromSuperview() } }
|
||||
didSet { children.forEach { addSubview($0) }; setNeedsLayout() }
|
||||
}
|
||||
|
||||
init(children: [ReliefButton]) {
|
||||
super.init()
|
||||
self.children = children
|
||||
}
|
||||
|
||||
override var isHighlighted: Bool {
|
||||
didSet {}
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
override func sizeThatFits(_ size: CGSize) -> CGSize {
|
||||
let width: CGFloat = children.reduce(0.0) { (result, button) -> CGFloat in
|
||||
return result + button.sizeThatFits(size).width
|
||||
}
|
||||
|
||||
return CGSize(width: width, height: size.height)
|
||||
}
|
||||
|
||||
override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
|
||||
backgroundView.frame = bounds
|
||||
shadowView.frame = bounds
|
||||
|
||||
var buttonRect = CGRect(origin: .zero, size: CGSize(width: 0, height: bounds.height))
|
||||
for child in children {
|
||||
child.shadowView.isHidden = true
|
||||
child.backgroundView.layer.borderWidth = 0
|
||||
bringSubviewToFront(child)
|
||||
|
||||
let childSize = child.sizeThatFits(bounds.size)
|
||||
buttonRect.size = CGSize(width: childSize.width, height: bounds.height)
|
||||
child.frame = buttonRect
|
||||
|
||||
buttonRect.origin.x += buttonRect.width
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user