Skip to content

Commit 0fa761a

Browse files
authored
Add TKUIHomeCard.config.prompt, which shows above the search bar (#417)
1 parent 1c57e6b commit 0fa761a

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

Sources/TripKitUI/cards/TKUIHomeCard+Configuration.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public extension TKUIHomeCard {
4141

4242
static let empty = Configuration()
4343

44+
/// Prompt to show above the search bar
45+
public var prompt: String? = nil
46+
4447
/// Set this to indicate if permission to access location services should be
4548
/// requested when the card is loaded. When setting to `true`, the map
4649
/// is zoomed to a user's current location and begins displaying nearby

Sources/TripKitUI/cards/TKUIHomeCard.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ open class TKUIHomeCard: TKUITableCard {
5555
hasGrabHandle = true
5656
#endif
5757

58-
self.headerView = TKUIHomeHeaderView(hasGrabHandle: hasGrabHandle)
58+
self.headerView = TKUIHomeHeaderView(
59+
hasGrabHandle: hasGrabHandle,
60+
prompt: TKUIHomeCard.config.prompt
61+
)
5962

6063
super.init(title: .custom(headerView, dismissButton: nil), mapManager: theMapManager, initialPosition: initialPosition)
6164

Sources/TripKitUI/views/TKUIHomeHeaderView.swift

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,28 @@
88

99
import UIKit
1010

11+
import TripKit
12+
1113
class TKUIHomeHeaderView: UIView {
1214

1315
var searchBar: UISearchBar!
1416
var directionsButton: UIButton?
1517

16-
private var stackView: UIStackView!
1718
private var directionsWrapper: UIView?
1819

1920
private let hasGrabHandle: Bool
21+
private let prompt: String?
2022

21-
init(hasGrabHandle: Bool) {
23+
init(hasGrabHandle: Bool, prompt: String? = nil) {
2224
self.hasGrabHandle = hasGrabHandle
25+
self.prompt = prompt
2326
super.init(frame: .zero)
2427
didInit()
2528
}
2629

2730
required init?(coder: NSCoder) {
2831
self.hasGrabHandle = true
32+
self.prompt = nil
2933
super.init(coder: coder)
3034
didInit()
3135
}
@@ -61,27 +65,43 @@ class TKUIHomeHeaderView: UIView {
6165
directionsWrapper.centerXAnchor.constraint(equalTo: directionsButton.centerXAnchor),
6266
])
6367

64-
let stackedViews: [UIView] = [searchBar, directionsWrapper]
65-
66-
let stackView = UIStackView(arrangedSubviews: stackedViews)
67-
stackView.translatesAutoresizingMaskIntoConstraints = false
68-
stackView.axis = .horizontal
69-
stackView.spacing = 0
70-
71-
addSubview(stackView)
68+
let hStack = UIStackView(arrangedSubviews: [searchBar, directionsWrapper])
69+
hStack.translatesAutoresizingMaskIntoConstraints = false
70+
hStack.axis = .horizontal
71+
hStack.spacing = 0
72+
73+
addSubview(hStack)
7274

73-
NSLayoutConstraint.activate([
74-
stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 6),
75-
stackView.topAnchor.constraint(equalTo: topAnchor, constant: hasGrabHandle ? -10 : 0), // negative spacer to minimise gap to grab handle
76-
trailingAnchor.constraint(equalTo: stackView.trailingAnchor, constant: 10),
77-
bottomAnchor.constraint(equalTo: stackView.bottomAnchor),
78-
])
75+
if let prompt {
76+
let label = UILabel()
77+
label.translatesAutoresizingMaskIntoConstraints = false
78+
label.font = TKStyleManager.boldCustomFont(forTextStyle: .title2)
79+
label.text = prompt
80+
addSubview(label)
81+
82+
NSLayoutConstraint.activate([
83+
label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 12),
84+
label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -12),
85+
hStack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 6),
86+
trailingAnchor.constraint(equalTo: hStack.trailingAnchor, constant: 10),
87+
88+
label.topAnchor.constraint(equalTo: topAnchor, constant: 0),
89+
hStack.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 0),
90+
bottomAnchor.constraint(equalTo: hStack.bottomAnchor),
91+
])
92+
93+
} else {
94+
NSLayoutConstraint.activate([
95+
hStack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 6),
96+
hStack.topAnchor.constraint(equalTo: topAnchor, constant: hasGrabHandle ? -10 : 0), // negative spacer to minimise gap to grab handle
97+
trailingAnchor.constraint(equalTo: hStack.trailingAnchor, constant: 10),
98+
bottomAnchor.constraint(equalTo: hStack.bottomAnchor),
99+
])
100+
}
79101

80-
self.stackView = stackView
81102
self.searchBar = searchBar
82103
self.directionsButton = directionsButton
83104

84-
// Styling
85105
searchBar.tintColor = .tkAppTintColor
86106
searchBar.barTintColor = .tkBackground
87107
}

0 commit comments

Comments
 (0)