Nicer toolbar buttons

This commit is contained in:
James Magahern
2020-08-14 15:55:08 -07:00
parent 9b36a3ace5
commit 69029a3195
12 changed files with 534 additions and 187 deletions

View File

@@ -10,11 +10,14 @@ import UIKit
class TitlebarView: UIView
{
private let titleLabelView = UILabel(frame: .zero)
private let backgroundImageView = UIImageView(frame: .zero)
private let backgroundView = GradientView(direction: .horizontal, colors: [
UIColor(red: 0.101, green: 0.176, blue: 0.415, alpha: 1.0),
UIColor(red: 0.153, green: 0.000, blue: 0.153, alpha: 1.0)
])
convenience init() {
self.init(frame: .zero)
addSubview(backgroundImageView)
addSubview(backgroundView)
addSubview(titleLabelView)
titleLabelView.textColor = .white
@@ -23,7 +26,7 @@ class TitlebarView: UIView
titleLabelView.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
titleLabelView.font = UIFont.boldSystemFont(ofSize: 12.0)
backgroundImageView.alpha = 0.98
backgroundView.alpha = 0.98
}
func setTitle(_ title: String) {
@@ -49,37 +52,10 @@ class TitlebarView: UIView
}
}
private func backgroundImageForSize(_ size: CGSize) -> UIImage? {
var image: UIImage? = nil
UIGraphicsBeginImageContext(CGSize(width: size.width, height: 1.0))
if let context = UIGraphicsGetCurrentContext() {
let gradientColorsArray = [
UIColor(red: 0.101, green: 0.176, blue: 0.415, alpha: 1.0).cgColor,
UIColor(red: 0.153, green: 0.000, blue: 0.153, alpha: 1.0).cgColor
]
if let gradient = CGGradient(colorsSpace: nil, colors: gradientColorsArray as CFArray, locations: nil) {
context.drawLinearGradient(gradient, start: .zero, end: CGPoint(x: size.width, y: 0.0), options: CGGradientDrawingOptions())
}
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
}
return image
}
override func layoutSubviews() {
super.layoutSubviews()
backgroundImageView.frame = bounds
backgroundView.frame = bounds
titleLabelView.frame = bounds.avoiding(verticalInsets: safeAreaInsets).insetBy(dx: 8.0 + layoutMargins.left, dy: 0.0)
if let image = backgroundImageView.image, image.size == bounds.size {
// No op
} else {
backgroundImageView.image = backgroundImageForSize(bounds.size)
}
}
}