@@ -139,6 +139,15 @@ open class BTNavigationDropdownMenu: UIView {
139
139
}
140
140
}
141
141
142
+ open var iconImage : UIImage ! {
143
+ get {
144
+ return self . configuration. iconImage
145
+ }
146
+ set ( value) {
147
+ self . configuration. iconImage = value
148
+ }
149
+ }
150
+
142
151
// The checkmark icon of the cell
143
152
open var checkMarkImage : UIImage ! {
144
153
get {
@@ -220,6 +229,15 @@ open class BTNavigationDropdownMenu: UIView {
220
229
}
221
230
}
222
231
232
+ open var shouldChangeTitleImage : Bool ! {
233
+ get {
234
+ return self . configuration. shouldChangeTitleImage
235
+ }
236
+ set ( value) {
237
+ self . configuration. shouldChangeTitleImage = value
238
+ }
239
+ }
240
+
223
241
open var didSelectItemAtIndexHandler : ( ( _ indexPath: Int ) -> ( ) ) ?
224
242
open var isShown : Bool !
225
243
@@ -229,9 +247,11 @@ open class BTNavigationDropdownMenu: UIView {
229
247
fileprivate var menuButton : UIButton !
230
248
fileprivate var menuTitle : UILabel !
231
249
fileprivate var menuArrow : UIImageView !
250
+ fileprivate var menuImage : UIImageView !
232
251
fileprivate var backgroundView : UIView !
233
252
fileprivate var tableView : BTTableView !
234
253
fileprivate var items : [ String ] !
254
+ fileprivate var itemImages : [ UIImage ] ?
235
255
fileprivate var menuWrapper : UIView !
236
256
237
257
required public init ? ( coder aDecoder: NSCoder ) {
@@ -248,9 +268,9 @@ open class BTNavigationDropdownMenu: UIView {
248
268
- title: A string to define title to be displayed.
249
269
- items: The array of items to select
250
270
*/
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 ) {
252
272
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 )
254
274
}
255
275
256
276
/**
@@ -265,7 +285,7 @@ open class BTNavigationDropdownMenu: UIView {
265
285
- title: An enum to define title to be displayed, can be a string or index of items.
266
286
- items: The array of items to select
267
287
*/
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 ) {
269
289
// Key window
270
290
guard let window = UIApplication . shared. keyWindow else {
271
291
super. init ( frame: CGRect . zero)
@@ -303,6 +323,7 @@ open class BTNavigationDropdownMenu: UIView {
303
323
304
324
self . isShown = false
305
325
self . items = items
326
+ self . itemImages = itemImages
306
327
307
328
// Init button as navigation title
308
329
self . menuButton = UIButton ( frame: frame)
@@ -319,6 +340,11 @@ open class BTNavigationDropdownMenu: UIView {
319
340
self . menuArrow = UIImageView ( image: self . configuration. arrowImage. withRenderingMode ( . alwaysTemplate) )
320
341
self . menuButton. addSubview ( self . menuArrow)
321
342
343
+ self . menuImage = UIImageView ( image: nil )
344
+ if ( self . itemImages != nil ) {
345
+ self . menuButton. addSubview ( self . menuImage)
346
+ }
347
+
322
348
let menuWrapperBounds = window. bounds
323
349
324
350
// Set up DropdownMenu
@@ -341,7 +367,7 @@ open class BTNavigationDropdownMenu: UIView {
341
367
// Init table view
342
368
let navBarHeight = self . navigationController? . navigationBar. bounds. size. height ?? 0
343
369
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)
345
371
346
372
self . tableView. selectRowAtIndexPathHandler = { [ weak self] ( indexPath: Int ) -> ( ) in
347
373
guard let selfie = self else {
@@ -351,6 +377,9 @@ open class BTNavigationDropdownMenu: UIView {
351
377
if selfie. shouldChangeTitleText! {
352
378
selfie. setMenuTitle ( " \( selfie. tableView. items [ indexPath] ) " )
353
379
}
380
+ if selfie. shouldChangeTitleImage!, let itemImages = selfie. tableView. itemImages {
381
+ selfie. setMenuImage ( itemImages [ indexPath] )
382
+ }
354
383
self ? . hideMenu ( )
355
384
self ? . layoutSubviews ( )
356
385
}
@@ -382,6 +411,8 @@ open class BTNavigationDropdownMenu: UIView {
382
411
self . menuTitle. textColor = self . configuration. menuTitleColor
383
412
self . menuArrow. sizeToFit ( )
384
413
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 )
385
416
self . menuWrapper. frame. origin. y = self . navigationController!. navigationBar. frame. maxY
386
417
self . tableView. reloadData ( )
387
418
}
@@ -413,13 +444,21 @@ open class BTNavigationDropdownMenu: UIView {
413
444
}
414
445
}
415
446
447
+ open func updateItemImages( _ images: [ UIImage ] ? ) {
448
+ self . tableView. itemImages = images
449
+ self . tableView. reloadData ( )
450
+ }
451
+
416
452
open func setSelected( index: Int ) {
417
453
self . tableView. selectedIndexPath = index
418
454
self . tableView. reloadData ( )
419
455
420
456
if self . shouldChangeTitleText! {
421
457
self . setMenuTitle ( " \( self . tableView. items [ index] ) " )
422
458
}
459
+ if self . shouldChangeTitleImage!, let itemImages = self . tableView. itemImages {
460
+ self . setMenuImage ( itemImages [ index] )
461
+ }
423
462
}
424
463
425
464
func setupDefaultConfiguration( ) {
@@ -520,6 +559,10 @@ open class BTNavigationDropdownMenu: UIView {
520
559
self . menuTitle. text = title
521
560
}
522
561
562
+ func setMenuImage( _ image: UIImage ) {
563
+ self . menuImage. image = image
564
+ }
565
+
523
566
@objc func menuButtonTapped( _ sender: UIButton ) {
524
567
self . isShown == true ? hideMenu ( ) : showMenu ( )
525
568
}
0 commit comments