blob: 3f5a91e1184051036ac4233c355e4fdec2b35a18 (
plain)
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
|
//
// CustomNavigationBar.swift
// MullvadVPN
//
// Created by pronebird on 22/05/2019.
// Copyright © 2019 Mullvad VPN AB. All rights reserved.
//
import UIKit
class CustomNavigationBar: UINavigationBar {
var prefersOpaqueBackground: Bool {
didSet {
setOpaqueBackgroundAppearance(prefersOpaqueBackground)
}
}
// Returns the distance from the title label to the bottom of navigation bar
var titleLabelBottomInset: CGFloat {
// Go two levels deep only
let subviewsToExamine = subviews.flatMap { (view) -> [UIView] in
return [view] + view.subviews
}
let titleLabel = subviewsToExamine.first { (view) -> Bool in
return view is UILabel
}
if let titleLabel = titleLabel {
let titleFrame = titleLabel.convert(titleLabel.bounds, to: self)
return max(bounds.maxY - titleFrame.maxY, 0)
} else {
return 0
}
}
override init(frame: CGRect) {
if #available(iOS 13, *) {
prefersOpaqueBackground = false
} else {
prefersOpaqueBackground = true
}
super.init(frame: frame)
var margins = layoutMargins
margins.left = UIMetrics.contentLayoutMargins.left
margins.right = UIMetrics.contentLayoutMargins.right
layoutMargins = margins
setOpaqueBackgroundAppearance(prefersOpaqueBackground)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setOpaqueBackgroundAppearance(_ flag: Bool) {
if flag {
barTintColor = .secondaryColor
backgroundColor = .secondaryColor
shadowImage = UIImage()
isTranslucent = false
} else {
barTintColor = nil
backgroundColor = nil
shadowImage = nil
isTranslucent = true
}
}
}
|