/
CustomNavigationBar.swift
157 lines (127 loc) · 4.74 KB
/
CustomNavigationBar.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// CustomNavigationBar.swift
// Recast
//
// Created by Jack Thompson on 11/14/18.
// Copyright © 2018 Cornell AppDev. All rights reserved.
//
import UIKit
class CustomNavigationBar: UIView {
// MARK: - Variables
private var titleLabel: UILabel!
private var subtitleLabel: UILabel!
private var backgroundView: UIView!
private var blurEffectView: UIVisualEffectView!
private var backgroundImage: UIImageView!
private var blurEffect = UIBlurEffect(style: .dark)
/// If true, navigation bar is slightly transparent with blur effect for contents below.
/// Otherwise bar is a solid color.
var isTranslucent: Bool = true {
didSet {
backgroundView.alpha = isTranslucent ? 0.6 : 1.0
blurEffectView.effect = isTranslucent ? blurEffect : nil
}
}
/// Color of the navigation bar.
var barColor: UIColor = .black {
didSet {
backgroundView.backgroundColor = barColor
}
}
/// Optional image for the background of the navigation bar.
var barImage: UIImage? {
didSet {
backgroundImage.image = barImage
}
}
/// Title of the navigation bar.
/// If not set, takes the value of `navigationItem.title` for the containing view controller.
var title: String? {
didSet {
titleLabel.text = title
}
}
/// Optional subtitle of the navigation bar.
/// If set, adjust bar in height to fit.
var subtitle: String? {
didSet {
subtitleLabel.text = subtitle
subtitleLabel.snp.remakeConstraints { make in
make.bottom.equalToSuperview().inset(bottomPadding)
make.leading.trailing.equalToSuperview().inset(edgePadding)
make.height.equalTo(titleLabel).multipliedBy(subtitleMultiplier)
}
}
}
// MARK: - Constants
static let height = 90 + UIApplication.shared.statusBarFrame.height
static let smallHeight = 50 + UIApplication.shared.statusBarFrame.height
let subtitleMultiplier = 0.75
let bottomPadding = 24
let edgePadding = 36
let minSubtitleHeight = 16
override init(frame: CGRect) {
super.init(frame: frame)
backgroundView = UIView()
backgroundView.alpha = 0.6
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = .zero
layer.shadowOpacity = 0.6
layer.shadowRadius = 4.0
blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
backgroundImage = UIImageView()
backgroundImage.contentMode = .scaleAspectFill
backgroundImage.alpha = 0.2
backgroundImage.clipsToBounds = true
backgroundView.addSubview(backgroundImage)
titleLabel = UILabel()
titleLabel.font = .boldSystemFont(ofSize: 72)
titleLabel.textColor = .white
titleLabel.textAlignment = .center
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.minimumScaleFactor = 0
titleLabel.numberOfLines = 0
subtitleLabel = UILabel()
subtitleLabel.font = .systemFont(ofSize: 72)
subtitleLabel.textColor = .gray
subtitleLabel.textAlignment = .center
subtitleLabel.adjustsFontSizeToFitWidth = true
subtitleLabel.minimumScaleFactor = 0
subtitleLabel.numberOfLines = 0
addSubview(backgroundView)
addSubview(blurEffectView)
addSubview(titleLabel)
addSubview(subtitleLabel)
setUpConstraints()
}
func setUpConstraints() {
// MARK: - Constants
let topPadding = UIApplication.shared.statusBarFrame.height + 10
let maxTitleHeight = 42
let minTitleHeight = 22
backgroundView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
blurEffectView.snp.makeConstraints { make in
make.edges.equalTo(backgroundView)
}
backgroundImage.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
titleLabel.snp.makeConstraints { make in
make.top.greaterThanOrEqualToSuperview().offset(topPadding)
make.bottom.equalTo(subtitleLabel.snp.top)
make.leading.trailing.equalToSuperview().inset(edgePadding)
make.height.greaterThanOrEqualTo(minTitleHeight)
make.height.lessThanOrEqualTo(maxTitleHeight)
}
subtitleLabel.snp.makeConstraints { make in
make.bottom.equalToSuperview().inset(bottomPadding)
make.leading.trailing.equalToSuperview().inset(edgePadding)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}