-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathBTNavigationDropdownMenu.swift
executable file
·691 lines (580 loc) · 25.8 KB
/
BTNavigationDropdownMenu.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//
// BTConfiguration.swift
// BTNavigationDropdownMenu
//
// Created by Pham Ba Tho on 6/30/15.
// Copyright (c) 2015 PHAM BA THO. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
// MARK: BTNavigationDropdownMenu
public class BTNavigationDropdownMenu: UIView {
// The color of menu title. Default is darkGrayColor()
public var menuTitleColor: UIColor! {
get {
return self.configuration.menuTitleColor
}
set(value) {
self.configuration.menuTitleColor = value
}
}
// The height of the cell. Default is 50
public var cellHeight: NSNumber! {
get {
return CGFloat(self.configuration.cellHeight)
}
set(value) {
self.configuration.cellHeight = CGFloat(value)
}
}
// The color of the cell background. Default is whiteColor()
public var cellBackgroundColor: UIColor! {
get {
return self.configuration.cellBackgroundColor
}
set(color) {
self.configuration.cellBackgroundColor = color
}
}
public var cellSeparatorColor: UIColor! {
get {
return self.configuration.cellSeparatorColor
}
set(value) {
self.configuration.cellSeparatorColor = value
}
}
// The color of the text inside cell. Default is darkGrayColor()
public var cellTextLabelColor: UIColor! {
get {
return self.configuration.cellTextLabelColor
}
set(value) {
self.configuration.cellTextLabelColor = value
}
}
// The font of the text inside cell. Default is HelveticaNeue-Bold, size 19
public var cellTextLabelFont: UIFont! {
get {
return self.configuration.cellTextLabelFont
}
set(value) {
self.configuration.cellTextLabelFont = value
self.menuTitle.font = self.configuration.cellTextLabelFont
}
}
// The alignment of the text inside cell. Default is .Left
public var cellTextLabelAlignment: NSTextAlignment! {
get {
return self.configuration.cellTextLabelAlignment
}
set(value) {
self.configuration.cellTextLabelAlignment = value
}
}
// The color of the cell when the cell is selected. Default is lightGrayColor()
public var cellSelectionColor: UIColor! {
get {
return self.configuration.cellSelectionColor
}
set(value) {
self.configuration.cellSelectionColor = value
}
}
// The checkmark icon of the cell
public var checkMarkImage: UIImage! {
get {
return self.configuration.checkMarkImage
}
set(value) {
self.configuration.checkMarkImage = value
}
}
public var keepSelectedCellColor: Bool! {
get {
return self.configuration.keepSelectedCellColor
}
set(value) {
self.configuration.keepSelectedCellColor = value
}
}
// The animation duration of showing/hiding menu. Default is 0.3
public var animationDuration: NSTimeInterval! {
get {
return self.configuration.animationDuration
}
set(value) {
self.configuration.animationDuration = value
}
}
// The arrow next to navigation title
public var arrowImage: UIImage! {
get {
return self.configuration.arrowImage
}
set(value) {
self.configuration.arrowImage = value
self.menuArrow.image = self.configuration.arrowImage
}
}
// The padding between navigation title and arrow
public var arrowPadding: CGFloat! {
get {
return self.configuration.arrowPadding
}
set(value) {
self.configuration.arrowPadding = value
}
}
// The color of the mask layer. Default is blackColor()
public var maskBackgroundColor: UIColor! {
get {
return self.configuration.maskBackgroundColor
}
set(value) {
self.configuration.maskBackgroundColor = value
}
}
// The opacity of the mask layer. Default is 0.3
public var maskBackgroundOpacity: CGFloat! {
get {
return self.configuration.maskBackgroundOpacity
}
set(value) {
self.configuration.maskBackgroundOpacity = value
}
}
public var didSelectItemAtIndexHandler: ((indexPath: Int) -> ())?
public var isShown: Bool!
private weak var navigationController: UINavigationController?
private var configuration = BTConfiguration()
private var topSeparator: UIView!
private var menuButton: UIButton!
private var menuTitle: UILabel!
private var menuArrow: UIImageView!
private var backgroundView: UIView!
private var tableView: BTTableView!
private var items: [BTItem]!
private var menuWrapper: UIView!
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@available(*, deprecated, message="Use init(navigationController:title:items:) instead", renamed="BTNavigationDropdownMenu(navigationController: UINavigationController?, title: String, items: [AnyObject])")
public convenience init(title: String, items: [BTItem]) {
self.init(navigationController: nil, title: title, items: items)
}
public init(navigationController: UINavigationController? = nil, title: String, items: [BTItem]) {
// Navigation controller
if let navigationController = navigationController {
self.navigationController = navigationController
} else {
self.navigationController = UIApplication.sharedApplication().keyWindow?.rootViewController?.topMostViewController?.navigationController
}
// Get titleSize
let titleSize = (title as NSString).sizeWithAttributes([NSFontAttributeName:self.configuration.cellTextLabelFont])
// Set frame
let frame = CGRectMake(0, 0, titleSize.width + (self.configuration.arrowPadding + self.configuration.arrowImage.size.width)*2, self.navigationController!.navigationBar.frame.height)
super.init(frame:frame)
self.isShown = false
self.items = items
// Init properties
self.setupDefaultConfiguration()
// Init button as navigation title
self.menuButton = UIButton(frame: frame)
self.menuButton.addTarget(self, action: #selector(BTNavigationDropdownMenu.menuButtonTapped(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.addSubview(self.menuButton)
self.menuTitle = UILabel(frame: frame)
self.menuTitle.text = title
self.menuTitle.textColor = self.menuTitleColor
self.menuTitle.font = self.configuration.cellTextLabelFont
self.menuTitle.textAlignment = self.configuration.cellTextLabelAlignment
self.menuButton.addSubview(self.menuTitle)
self.menuArrow = UIImageView(image: self.configuration.arrowImage)
self.menuButton.addSubview(self.menuArrow)
let window = UIApplication.sharedApplication().keyWindow!
let menuWrapperBounds = window.bounds
// Set up DropdownMenu
self.menuWrapper = UIView(frame: CGRectMake(menuWrapperBounds.origin.x, 0, menuWrapperBounds.width, menuWrapperBounds.height))
self.menuWrapper.clipsToBounds = true
self.menuWrapper.autoresizingMask = [ .FlexibleWidth, .FlexibleHeight ]
// Init background view (under table view)
self.backgroundView = UIView(frame: menuWrapperBounds)
self.backgroundView.backgroundColor = self.configuration.maskBackgroundColor
self.backgroundView.autoresizingMask = [ .FlexibleWidth, .FlexibleHeight ]
let backgroundTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(BTNavigationDropdownMenu.hideMenu));
self.backgroundView.addGestureRecognizer(backgroundTapRecognizer)
// Init table view
self.tableView = BTTableView(frame: CGRectMake(menuWrapperBounds.origin.x, menuWrapperBounds.origin.y + 0.5, menuWrapperBounds.width, menuWrapperBounds.height + 300), items: items, title: title, configuration: self.configuration)
self.tableView.selectRowAtIndexPathHandler = { [weak self] (indexPath: Int) -> () in
self?.didSelectItemAtIndexHandler!(indexPath: indexPath)
self?.setMenuTitle("\(items[indexPath].title)")
self?.hideMenu()
self?.layoutSubviews()
}
// Add background view & table view to container view
self.menuWrapper.addSubview(self.backgroundView)
self.menuWrapper.addSubview(self.tableView)
// Add Line on top
self.topSeparator = UIView(frame: CGRectMake(0, 0, menuWrapperBounds.size.width, 0.5))
self.topSeparator.autoresizingMask = UIViewAutoresizing.FlexibleWidth
self.menuWrapper.addSubview(self.topSeparator)
// Add Menu View to container view
window.addSubview(self.menuWrapper)
// By default, hide menu view
self.menuWrapper.hidden = true
}
override public func layoutSubviews() {
self.menuTitle.sizeToFit()
self.menuTitle.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2)
self.menuTitle.textColor = self.configuration.menuTitleColor
self.menuArrow.sizeToFit()
self.menuArrow.center = CGPointMake(CGRectGetMaxX(self.menuTitle.frame) + self.configuration.arrowPadding, self.frame.size.height/2)
self.menuWrapper.frame.origin.y = self.navigationController!.navigationBar.frame.maxY
self.tableView.reloadData()
}
public func show() {
if self.isShown == false {
self.showMenu()
}
}
public func hide() {
if self.isShown == true {
self.hideMenu()
}
}
public func toggle() {
if(self.isShown == true) {
self.hideMenu();
} else {
self.showMenu();
}
}
func setupDefaultConfiguration() {
self.menuTitleColor = self.navigationController?.navigationBar.titleTextAttributes?[NSForegroundColorAttributeName] as? UIColor // Setter
self.cellBackgroundColor = self.navigationController?.navigationBar.barTintColor
self.cellSeparatorColor = self.navigationController?.navigationBar.titleTextAttributes?[NSForegroundColorAttributeName] as? UIColor
self.cellTextLabelColor = self.navigationController?.navigationBar.titleTextAttributes?[NSForegroundColorAttributeName] as? UIColor
}
func showMenu() {
self.menuWrapper.frame.origin.y = self.navigationController!.navigationBar.frame.maxY
self.isShown = true
// Table view header
let headerView = UIView(frame: CGRectMake(0, 0, self.frame.width, 300))
headerView.backgroundColor = self.configuration.cellBackgroundColor
self.tableView.tableHeaderView = headerView
self.topSeparator.backgroundColor = self.configuration.cellSeparatorColor
// Rotate arrow
self.rotateArrow()
// Visible menu view
self.menuWrapper.hidden = false
// Change background alpha
self.backgroundView.alpha = 0
// Animation
self.tableView.frame.origin.y = -CGFloat(self.items.count) * self.configuration.cellHeight - 300
// Reload data to dismiss highlight color of selected cell
self.tableView.reloadData()
self.menuWrapper.superview?.bringSubviewToFront(self.menuWrapper)
UIView.animateWithDuration(
self.configuration.animationDuration * 1.5,
delay: 0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.5,
options: [],
animations: {
self.tableView.frame.origin.y = CGFloat(-300)
self.backgroundView.alpha = self.configuration.maskBackgroundOpacity
}, completion: nil
)
}
func hideMenu() {
// Rotate arrow
self.rotateArrow()
self.isShown = false
// Change background alpha
self.backgroundView.alpha = self.configuration.maskBackgroundOpacity
UIView.animateWithDuration(
self.configuration.animationDuration * 1.5,
delay: 0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.5,
options: [],
animations: {
self.tableView.frame.origin.y = CGFloat(-200)
}, completion: nil
)
// Animation
UIView.animateWithDuration(
self.configuration.animationDuration,
delay: 0,
options: UIViewAnimationOptions.TransitionNone,
animations: {
self.tableView.frame.origin.y = -CGFloat(self.items.count) * self.configuration.cellHeight - 300
self.backgroundView.alpha = 0
}, completion: { _ in
if self.isShown == false && self.tableView.frame.origin.y == -CGFloat(self.items.count) * self.configuration.cellHeight - 300 {
self.menuWrapper.hidden = true
}
})
}
func rotateArrow() {
UIView.animateWithDuration(self.configuration.animationDuration, animations: {[weak self] () -> () in
if let selfie = self {
selfie.menuArrow.transform = CGAffineTransformRotate(selfie.menuArrow.transform, 180 * CGFloat(M_PI/180))
}
})
}
func setMenuTitle(title: String) {
self.menuTitle.text = title
}
func menuButtonTapped(sender: UIButton) {
self.isShown == true ? hideMenu() : showMenu()
}
}
// MARK: BTConfiguration
class BTConfiguration {
var menuTitleColor: UIColor?
var cellHeight: CGFloat!
var cellBackgroundColor: UIColor?
var cellSeparatorColor: UIColor?
var cellTextLabelColor: UIColor?
var cellTextLabelFont: UIFont!
var cellTextLabelAlignment: NSTextAlignment!
var cellSelectionColor: UIColor?
var checkMarkImage: UIImage!
var keepSelectedCellColor: Bool!
var arrowImage: UIImage!
var arrowPadding: CGFloat!
var animationDuration: NSTimeInterval!
var maskBackgroundColor: UIColor!
var maskBackgroundOpacity: CGFloat!
init() {
self.defaultValue()
}
func defaultValue() {
// Path for image
let bundle = NSBundle(forClass: BTConfiguration.self)
let url = bundle.URLForResource("BTNavigationDropdownMenu", withExtension: "bundle")
let imageBundle = NSBundle(URL: url!)
let checkMarkImagePath = imageBundle?.pathForResource("checkmark_icon", ofType: "png")
let arrowImagePath = imageBundle?.pathForResource("arrow_down_icon", ofType: "png")
// Default values
self.menuTitleColor = UIColor.darkGrayColor()
self.cellHeight = 50
self.cellBackgroundColor = UIColor.whiteColor()
self.cellSeparatorColor = UIColor.darkGrayColor()
self.cellTextLabelColor = UIColor.darkGrayColor()
self.cellTextLabelFont = UIFont(name: "HelveticaNeue-Bold", size: 17)
self.cellTextLabelAlignment = NSTextAlignment.Left
self.cellSelectionColor = UIColor.lightGrayColor()
self.checkMarkImage = UIImage(contentsOfFile: checkMarkImagePath!)
self.keepSelectedCellColor = false
self.animationDuration = 0.5
self.arrowImage = UIImage(contentsOfFile: arrowImagePath!)
self.arrowPadding = 15
self.maskBackgroundColor = UIColor.blackColor()
self.maskBackgroundOpacity = 0.3
}
}
// MARK: Table View
class BTTableView: UITableView, UITableViewDelegate, UITableViewDataSource {
// Public properties
var configuration: BTConfiguration!
var selectRowAtIndexPathHandler: ((indexPath: Int) -> ())?
// Private properties
private var items: [BTItem]!
private var selectedIndexPath: Int!
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(frame: CGRect, items: [BTItem], title: String, configuration: BTConfiguration) {
super.init(frame: frame, style: UITableViewStyle.Plain)
self.items = items
self.selectedIndexPath = items.indexOf() { $0.title == title }
self.configuration = configuration
// Setup table view
self.delegate = self
self.dataSource = self
self.backgroundColor = UIColor.clearColor()
self.separatorStyle = UITableViewCellSeparatorStyle.None
// self.separatorEffect = UIBlurEffect(style: .Light)
self.autoresizingMask = UIViewAutoresizing.FlexibleWidth
self.tableFooterView = UIView(frame: CGRectZero)
}
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
if let hitView = super.hitTest(point, withEvent: event) where hitView.isKindOfClass(BTTableCellContentView.self) {
return hitView
}
return nil;
}
// Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return self.configuration.cellHeight
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let item = self.items[indexPath.row]
let cell = BTTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell", configuration: self.configuration, item: item)
cell.imageView?.image = item.image
cell.textLabel?.text = item.title
cell.checkmarkIcon.hidden = (indexPath.row == selectedIndexPath) ? false : true
if self.configuration.keepSelectedCellColor == true {
cell.contentView.backgroundColor = (indexPath.row == selectedIndexPath) ? self.configuration.cellSelectionColor : self.configuration.cellBackgroundColor
}
return cell
}
// Table view delegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedIndexPath = indexPath.row
self.selectRowAtIndexPathHandler!(indexPath: indexPath.row)
self.reloadData()
let cell = tableView.cellForRowAtIndexPath(indexPath) as? BTTableViewCell
cell?.contentView.backgroundColor = self.configuration.cellSelectionColor
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as? BTTableViewCell
cell?.checkmarkIcon.hidden = true
cell?.contentView.backgroundColor = self.configuration.cellBackgroundColor
}
}
// MARK: Table view cell
class BTTableViewCell: UITableViewCell {
let checkmarkIconWidth: CGFloat = 50
let horizontalMargin: CGFloat = 20
let imageMargin: CGFloat = 10
var checkmarkIcon: UIImageView!
var cellContentFrame: CGRect!
var configuration: BTConfiguration!
init(style: UITableViewCellStyle, reuseIdentifier: String?, configuration: BTConfiguration, item: BTItem) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.configuration = configuration
// Setup cell
cellContentFrame = CGRectMake(0, 0, (UIApplication.sharedApplication().keyWindow?.frame.width)!, self.configuration.cellHeight)
self.contentView.backgroundColor = self.configuration.cellBackgroundColor
self.selectionStyle = UITableViewCellSelectionStyle.None
self.imageView?.frame = CGRectMake(horizontalMargin, (cellContentFrame.height - 30)/2, 30, 30)
self.imageView?.contentMode = .ScaleAspectFit
self.textLabel!.textColor = self.configuration.cellTextLabelColor
self.textLabel!.font = self.configuration.cellTextLabelFont
self.textLabel!.textAlignment = self.configuration.cellTextLabelAlignment
if self.textLabel!.textAlignment == .Center {
self.textLabel!.frame = CGRectMake(0, 0, cellContentFrame.width, cellContentFrame.height)
} else if self.textLabel!.textAlignment == .Left {
var x = horizontalMargin
if let _ = item.image, width = imageView?.frame.size.width {
x = x + width + imageMargin
}
self.textLabel!.frame = CGRectMake(x, 0, cellContentFrame.width, cellContentFrame.height)
} else {
self.textLabel!.frame = CGRectMake(-horizontalMargin, 0, cellContentFrame.width, cellContentFrame.height)
}
// Checkmark icon
if self.textLabel!.textAlignment == .Center {
self.checkmarkIcon = UIImageView(frame: CGRectMake(cellContentFrame.width - checkmarkIconWidth, (cellContentFrame.height - 30)/2, 30, 30))
} else if self.textLabel!.textAlignment == .Left {
self.checkmarkIcon = UIImageView(frame: CGRectMake(cellContentFrame.width - checkmarkIconWidth, (cellContentFrame.height - 30)/2, 30, 30))
} else {
self.checkmarkIcon = UIImageView(frame: CGRectMake(horizontalMargin, (cellContentFrame.height - 30)/2, 30, 30))
}
self.checkmarkIcon.hidden = true
self.checkmarkIcon.image = self.configuration.checkMarkImage
self.checkmarkIcon.contentMode = UIViewContentMode.ScaleAspectFill
self.contentView.addSubview(self.checkmarkIcon)
// Separator for cell
let separator = BTTableCellContentView(frame: cellContentFrame)
if let cellSeparatorColor = self.configuration.cellSeparatorColor {
separator.separatorColor = cellSeparatorColor
}
self.contentView.addSubview(separator)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
self.bounds = cellContentFrame
self.contentView.frame = self.bounds
}
}
public struct BTItem {
public let image: UIImage?
public let title: String
public init(image: UIImage?, title: String) {
self.image = image
self.title = title
}
}
// Content view of table view cell
class BTTableCellContentView: UIView {
var separatorColor: UIColor = UIColor.blackColor()
override init(frame: CGRect) {
super.init(frame: frame)
self.initialize()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initialize()
}
func initialize() {
self.backgroundColor = UIColor.clearColor()
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
let context = UIGraphicsGetCurrentContext()
// Set separator color of dropdown menu based on barStyle
CGContextSetStrokeColorWithColor(context, self.separatorColor.CGColor)
CGContextSetLineWidth(context, 1)
CGContextMoveToPoint(context, 0, self.bounds.size.height)
CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height)
CGContextStrokePath(context)
}
}
extension UIViewController {
// Get ViewController in top present level
var topPresentedViewController: UIViewController? {
var target: UIViewController? = self
while (target?.presentedViewController != nil) {
target = target?.presentedViewController
}
return target
}
// Get top VisibleViewController from ViewController stack in same present level.
// It should be visibleViewController if self is a UINavigationController instance
// It should be selectedViewController if self is a UITabBarController instance
var topVisibleViewController: UIViewController? {
if let navigation = self as? UINavigationController {
if let visibleViewController = navigation.visibleViewController {
return visibleViewController.topVisibleViewController
}
}
if let tab = self as? UITabBarController {
if let selectedViewController = tab.selectedViewController {
return selectedViewController.topVisibleViewController
}
}
return self
}
// Combine both topPresentedViewController and topVisibleViewController methods, to get top visible viewcontroller in top present level
var topMostViewController: UIViewController? {
return self.topPresentedViewController?.topVisibleViewController
}
}