Skip to content

Commit 57a6c06

Browse files
author
Beata Lin
committed
Support cell image and title icon
1 parent 1c417c1 commit 57a6c06

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

Source/BTNavigationDropdownMenu.swift

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ open class BTNavigationDropdownMenu: UIView {
139139
}
140140
}
141141

142+
open var iconImage: UIImage! {
143+
get {
144+
return self.configuration.iconImage
145+
}
146+
set(value) {
147+
self.configuration.iconImage = value
148+
}
149+
}
150+
142151
// The checkmark icon of the cell
143152
open var checkMarkImage: UIImage! {
144153
get {
@@ -220,6 +229,15 @@ open class BTNavigationDropdownMenu: UIView {
220229
}
221230
}
222231

232+
open var shouldChangeTitleImage: Bool! {
233+
get {
234+
return self.configuration.shouldChangeTitleImage
235+
}
236+
set(value) {
237+
self.configuration.shouldChangeTitleImage = value
238+
}
239+
}
240+
223241
open var didSelectItemAtIndexHandler: ((_ indexPath: Int) -> ())?
224242
open var isShown: Bool!
225243

@@ -229,9 +247,11 @@ open class BTNavigationDropdownMenu: UIView {
229247
fileprivate var menuButton: UIButton!
230248
fileprivate var menuTitle: UILabel!
231249
fileprivate var menuArrow: UIImageView!
250+
fileprivate var menuImage: UIImageView!
232251
fileprivate var backgroundView: UIView!
233252
fileprivate var tableView: BTTableView!
234253
fileprivate var items: [String]!
254+
fileprivate var itemImages: [UIImage]?
235255
fileprivate var menuWrapper: UIView!
236256

237257
required public init?(coder aDecoder: NSCoder) {
@@ -248,9 +268,9 @@ open class BTNavigationDropdownMenu: UIView {
248268
- title: A string to define title to be displayed.
249269
- items: The array of items to select
250270
*/
251-
public convenience init(navigationController: UINavigationController? = nil, containerView: UIView = UIApplication.shared.keyWindow!, title: String, items: [String]) {
271+
public convenience init(navigationController: UINavigationController? = nil, containerView: UIView = UIApplication.shared.keyWindow!, title: String, items: [String], itemImages: [UIImage]? = nil) {
252272

253-
self.init(navigationController: navigationController, containerView: containerView, title: BTTitle.title(title), items: items)
273+
self.init(navigationController: navigationController, containerView: containerView, title: BTTitle.title(title), items: items, itemImages: itemImages)
254274
}
255275

256276
/**
@@ -265,7 +285,7 @@ open class BTNavigationDropdownMenu: UIView {
265285
- title: An enum to define title to be displayed, can be a string or index of items.
266286
- items: The array of items to select
267287
*/
268-
public init(navigationController: UINavigationController? = nil, containerView: UIView = UIApplication.shared.keyWindow!, title: BTTitle, items: [String]) {
288+
public init(navigationController: UINavigationController? = nil, containerView: UIView = UIApplication.shared.keyWindow!, title: BTTitle, items: [String], itemImages: [UIImage]? = nil) {
269289
// Key window
270290
guard let window = UIApplication.shared.keyWindow else {
271291
super.init(frame: CGRect.zero)
@@ -303,6 +323,7 @@ open class BTNavigationDropdownMenu: UIView {
303323

304324
self.isShown = false
305325
self.items = items
326+
self.itemImages = itemImages
306327

307328
// Init button as navigation title
308329
self.menuButton = UIButton(frame: frame)
@@ -319,6 +340,11 @@ open class BTNavigationDropdownMenu: UIView {
319340
self.menuArrow = UIImageView(image: self.configuration.arrowImage.withRenderingMode(.alwaysTemplate))
320341
self.menuButton.addSubview(self.menuArrow)
321342

343+
self.menuImage = UIImageView(image: nil)
344+
if (self.itemImages != nil) {
345+
self.menuButton.addSubview(self.menuImage)
346+
}
347+
322348
let menuWrapperBounds = window.bounds
323349

324350
// Set up DropdownMenu
@@ -341,7 +367,7 @@ open class BTNavigationDropdownMenu: UIView {
341367
// Init table view
342368
let navBarHeight = self.navigationController?.navigationBar.bounds.size.height ?? 0
343369
let statusBarHeight = UIApplication.shared.statusBarFrame.height
344-
self.tableView = BTTableView(frame: CGRect(x: menuWrapperBounds.origin.x, y: menuWrapperBounds.origin.y + 0.5, width: menuWrapperBounds.width, height: menuWrapperBounds.height + 300 - navBarHeight - statusBarHeight), items: items, title: titleToDisplay, configuration: self.configuration)
370+
self.tableView = BTTableView(frame: CGRect(x: menuWrapperBounds.origin.x, y: menuWrapperBounds.origin.y + 0.5, width: menuWrapperBounds.width, height: menuWrapperBounds.height + 300 - navBarHeight - statusBarHeight), items: items, itemImages: itemImages, title: titleToDisplay, configuration: self.configuration)
345371

346372
self.tableView.selectRowAtIndexPathHandler = { [weak self] (indexPath: Int) -> () in
347373
guard let selfie = self else {
@@ -351,6 +377,9 @@ open class BTNavigationDropdownMenu: UIView {
351377
if selfie.shouldChangeTitleText! {
352378
selfie.setMenuTitle("\(selfie.tableView.items[indexPath])")
353379
}
380+
if selfie.shouldChangeTitleImage!, let itemImages = selfie.tableView.itemImages {
381+
selfie.setMenuImage(itemImages[indexPath])
382+
}
354383
self?.hideMenu()
355384
self?.layoutSubviews()
356385
}
@@ -382,6 +411,8 @@ open class BTNavigationDropdownMenu: UIView {
382411
self.menuTitle.textColor = self.configuration.menuTitleColor
383412
self.menuArrow.sizeToFit()
384413
self.menuArrow.center = CGPoint(x: self.menuTitle.frame.maxX + self.configuration.arrowPadding, y: self.frame.size.height/2)
414+
self.menuImage.sizeToFit()
415+
self.menuImage.center = CGPoint(x: self.menuTitle.frame.minX - ((self.menuImage.image?.size.width ?? 0) / 2), y: self.frame.size.height/2)
385416
self.menuWrapper.frame.origin.y = self.navigationController!.navigationBar.frame.maxY
386417
self.tableView.reloadData()
387418
}
@@ -413,13 +444,21 @@ open class BTNavigationDropdownMenu: UIView {
413444
}
414445
}
415446

447+
open func updateItemImages(_ images: [UIImage]?) {
448+
self.tableView.itemImages = images
449+
self.tableView.reloadData()
450+
}
451+
416452
open func setSelected(index: Int) {
417453
self.tableView.selectedIndexPath = index
418454
self.tableView.reloadData()
419455

420456
if self.shouldChangeTitleText! {
421457
self.setMenuTitle("\(self.tableView.items[index])")
422458
}
459+
if self.shouldChangeTitleImage!, let itemImages = self.tableView.itemImages {
460+
self.setMenuImage(itemImages[index])
461+
}
423462
}
424463

425464
func setupDefaultConfiguration() {
@@ -520,6 +559,10 @@ open class BTNavigationDropdownMenu: UIView {
520559
self.menuTitle.text = title
521560
}
522561

562+
func setMenuImage(_ image: UIImage) {
563+
self.menuImage.image = image
564+
}
565+
523566
@objc func menuButtonTapped(_ sender: UIButton) {
524567
self.isShown == true ? hideMenu() : showMenu()
525568
}

Source/Internal/BTConfiguration.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ final class BTConfiguration {
3434
var navigationBarTitleFont: UIFont!
3535
var cellTextLabelAlignment: NSTextAlignment!
3636
var cellSelectionColor: UIColor?
37+
var iconImage: UIImage!
3738
var checkMarkImage: UIImage!
3839
var shouldKeepSelectedCellColor: Bool!
3940
var arrowTintColor: UIColor?
@@ -43,6 +44,7 @@ final class BTConfiguration {
4344
var maskBackgroundColor: UIColor!
4445
var maskBackgroundOpacity: CGFloat!
4546
var shouldChangeTitleText: Bool!
47+
var shouldChangeTitleImage: Bool!
4648

4749
init() {
4850
self.defaultValue()
@@ -69,12 +71,14 @@ final class BTConfiguration {
6971
self.cellTextLabelAlignment = NSTextAlignment.left
7072
self.cellSelectionColor = UIColor.lightGray
7173
self.checkMarkImage = UIImage(contentsOfFile: checkMarkImagePath!)
74+
self.iconImage = UIImage(contentsOfFile: checkMarkImagePath!)
7275
self.shouldKeepSelectedCellColor = false
7376
self.animationDuration = 0.5
7477
self.arrowImage = UIImage(contentsOfFile: arrowImagePath!)
7578
self.arrowPadding = 15
7679
self.maskBackgroundColor = UIColor.black
7780
self.maskBackgroundOpacity = 0.3
7881
self.shouldChangeTitleText = true
82+
self.shouldChangeTitleImage = false
7983
}
8084
}

Source/Internal/BTTableView.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,18 @@ class BTTableView: UITableView, UITableViewDelegate, UITableViewDataSource {
3131

3232
// Private properties
3333
var items: [String] = []
34+
var itemImages: [UIImage]? = nil
3435
var selectedIndexPath: Int?
3536

3637
required init?(coder aDecoder: NSCoder) {
3738
fatalError("init(coder:) has not been implemented")
3839
}
3940

40-
init(frame: CGRect, items: [String], title: String, configuration: BTConfiguration) {
41+
init(frame: CGRect, items: [String], itemImages: [UIImage]? = nil, title: String, configuration: BTConfiguration) {
4142
super.init(frame: frame, style: UITableView.Style.plain)
4243

4344
self.items = items
45+
self.itemImages = itemImages
4446
self.selectedIndexPath = items.index(of: title)
4547
self.configuration = configuration
4648

@@ -78,6 +80,8 @@ class BTTableView: UITableView, UITableViewDelegate, UITableViewDataSource {
7880
let cell = BTTableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "Cell", configuration: self.configuration)
7981
cell.textLabel?.text = self.items[(indexPath as NSIndexPath).row]
8082
cell.checkmarkIcon.isHidden = ((indexPath as NSIndexPath).row == selectedIndexPath) ? false : true
83+
cell.icon.image = self.itemImages?[indexPath.row]
84+
8185
return cell
8286
}
8387

Source/Internal/BTTableViewCell.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class BTTableViewCell: UITableViewCell {
2727
let checkmarkIconWidth: CGFloat = 50
2828
let horizontalMargin: CGFloat = 20
2929

30+
var icon: UIImageView!
3031
var checkmarkIcon: UIImageView!
3132
var cellContentFrame: CGRect!
3233
var configuration: BTConfiguration!
@@ -64,6 +65,16 @@ class BTTableViewCell: UITableViewCell {
6465
self.checkmarkIcon.contentMode = UIView.ContentMode.scaleAspectFill
6566
self.contentView.addSubview(self.checkmarkIcon)
6667

68+
self.icon = UIImageView(frame: CGRect(x: horizontalMargin - 15, y: (cellContentFrame.height - 30)/2, width: 30, height: 30))
69+
self.icon.contentMode = UIView.ContentMode.scaleAspectFill
70+
self.contentView.addSubview(self.icon)
71+
if let image = self.configuration.iconImage {
72+
self.icon.image = image
73+
if self.textLabel!.textAlignment == .left {
74+
self.textLabel!.frame = CGRect(x: self.icon.frame.maxX, y: 0, width: cellContentFrame.width, height: cellContentFrame.height)
75+
}
76+
}
77+
6778
// Separator for cell
6879
let separator = BTTableCellContentView(frame: cellContentFrame)
6980
if let cellSeparatorColor = self.configuration.cellSeparatorColor {

0 commit comments

Comments
 (0)