Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions Source/BTNavigationDropdownMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ open class BTNavigationDropdownMenu: UIView {
}

// Get titleSize
let titleSize: CGSize
var titleSize = CGSize.zero
let titleToDisplay: String

switch title{
Expand All @@ -294,7 +294,9 @@ open class BTNavigationDropdownMenu: UIView {
titleToDisplay = title
}

titleSize = (titleToDisplay as NSString).size(attributes: [NSFontAttributeName:self.configuration.navigationBarTitleFont])
if let maxTitle = items.max(by: {$1.characters.count > $0.characters.count}) {
titleSize = (maxTitle as NSString).size(attributes: [NSFontAttributeName:self.configuration.navigationBarTitleFont])
}

// Set frame
let frame = CGRect(x: 0, y: 0, width: titleSize.width + (self.configuration.arrowPadding + self.configuration.arrowImage.size.width)*2, height: self.navigationController!.navigationBar.frame.height)
Expand All @@ -308,7 +310,7 @@ open class BTNavigationDropdownMenu: UIView {
self.menuButton = UIButton(frame: frame)
self.menuButton.addTarget(self, action: #selector(BTNavigationDropdownMenu.menuButtonTapped(_:)), for: UIControlEvents.touchUpInside)
self.addSubview(self.menuButton)

self.menuTitle = UILabel(frame: frame)
self.menuTitle.text = titleToDisplay
self.menuTitle.textColor = self.menuTitleColor
Expand Down Expand Up @@ -413,14 +415,23 @@ open class BTNavigationDropdownMenu: UIView {
}
}

open func setSelected(index: Int) {
open func setSelected(index: Int?) {
self.tableView.selectedIndexPath = index
self.tableView.reloadData()

guard let index = index else {
self.setMenuTitle("")
return
}

if self.shouldChangeTitleText! {
self.setMenuTitle("\(self.tableView.items[index])")
}
}

open func setSelectedByTitle(title: String) {
self.setSelected(index: self.items.index(of: title))
}

func setupDefaultConfiguration() {
self.menuTitleColor = self.navigationController?.navigationBar.titleTextAttributes?[NSForegroundColorAttributeName] as? UIColor
Expand Down