Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set a maximum number of items displayed in dropdown or a maximum height for dropdown? #337

Open
DanHozan123 opened this issue Aug 14, 2024 · 1 comment

Comments

@DanHozan123
Copy link

No description provided.

@DanHozan123
Copy link
Author

here is the code that you have to add so you can have a limit of items displayed and the content scrollable

add this property:
public var maxVisibleItems: Int = 5 {
didSet {
self.setNeedsUpdateConstraints()
}
}

edit tableHeight and updateConstraints:

fileprivate var tableHeight: CGFloat {
let visibleItemsCount = min(dataSource.count, maxVisibleItems)
return tableView.rowHeight * CGFloat(visibleItemsCount)
}

public override func updateConstraints() {
if !didSetupConstraints {
setupConstraints()
}

didSetupConstraints = true

let layout = computeLayout()

if !layout.canBeDisplayed {
    super.updateConstraints()
    hide()

    return
}

xConstraint.constant = layout.x
yConstraint.constant = layout.y
widthConstraint.constant = layout.width
heightConstraint.constant = layout.visibleHeight

tableView.isScrollEnabled = layout.offscreenHeight > 0 || dataSource.count > maxVisibleItems

DispatchQueue.main.async { [weak self] in
    self?.tableView.flashScrollIndicators()
}

super.updateConstraints()

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant