// // 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 } } }