qmathe / DropDownMenuKit

UIKit drop down menu, simple yet flexible and written in Swift
Other
303 stars 38 forks source link

Do you have an example code for the 'sort' drop down you show in the screen shot? #30

Open netsmith opened 4 years ago

netsmith commented 4 years ago

the version where there is up and down arrows and also the segment where you select what you want to sort on? Its spot on what I'm looking for.

Thanks

netsmith commented 4 years ago

ie this one - https://camo.githubusercontent.com/ca29b98193b03d95dd691c9ec8e2308b2a8b4f54/687474703a2f2f7777772e7175656e74696e6d617468652e636f6d2f6769746875622f44726f70446f776e4d656e754b69742f506c6163652532304c697374253230416374696f6e2532304d656e752532302d2532306950686f6e65253230352e706e67

Sorry to chase Im about to implement it using a different library but I much prefer your :)

qmathe commented 4 years ago

Hello,

Here is the code I was using for the menu visible in the screenshot you're referring to:

let sortCell = DropDownMenuCell()
let sortSwitcher = UISegmentedControl(items: localizedSortingChoices)

allowedSortingKeys = ["name", "rating", "distance", "creationDate"]

sortSwitcher.apportionsSegmentWidthsByContent = true
sortSwitcher.selectedSegmentIndex = allowedSortingKeys.index(of: sortingKey)!
sortSwitcher.addTarget(self, action: #selector(PlaceListController.sort(_:)), for: .valueChanged)

sortCell.customView = sortSwitcher
sortCell.textLabel!.text = "Sort"
sortCell.imageView!.image = UIImage(named: "SortingArrows")
sortCell.showsCheckmark = false
sortCell.layoutMargins = .zero
sortCell.preservesSuperviewLayoutMargins = false

dropDownMenu.menuCells = [selectCell, sortCell]

The code was written for iOS 8 to 11, I haven't tested it for more recent iOS versions, so you might need to adjust it.