diff --git a/DropDown/src/DropDown.swift b/DropDown/src/DropDown.swift index f41e8ddd..518e15cf 100644 --- a/DropDown/src/DropDown.swift +++ b/DropDown/src/DropDown.swift @@ -749,13 +749,36 @@ extension DropDown { let x = anchorViewX + topOffset.x var y = (anchorViewMaxY + topOffset.y) - tableHeight - let windowY = window.bounds.minY + DPDConstant.UI.HeightPadding + var windowY = window.bounds.minY + if #available(iOS 11.0, *) { + windowY += window.safeAreaInsets.top + } else { + windowY += DPDConstant.UI.HeightPadding + } + + if y < windowY { + offscreenHeight = abs(y - windowY) + y = windowY + } else { + let maxY = y + tableHeight + + var windowMaxY = window.bounds.maxY - offsetFromWindowBottom + if #available(iOS 11.0, *) { + windowMaxY -= window.safeAreaInsets.bottom + } else { + windowMaxY -= DPDConstant.UI.HeightPadding + } - if y < windowY { - offscreenHeight = abs(y - windowY) - y = windowY + let keyboardListener = KeyboardListener.sharedInstance + let keyboardMinY = keyboardListener.keyboardFrame.minY - DPDConstant.UI.HeightPadding + + if keyboardListener.isVisible && maxY > keyboardMinY { + offscreenHeight = abs(maxY - keyboardMinY) + } else if maxY > windowMaxY { + offscreenHeight = abs(maxY - windowMaxY) + } } - + let width = self.width ?? (anchorView?.plainView.bounds.width ?? fittingWidth()) - topOffset.x return (x, y, width, offscreenHeight) @@ -973,10 +996,10 @@ extension DropDown { } /// (Pre)selects a row at a certain index. - public func selectRow(at index: Index?, scrollPosition: UITableView.ScrollPosition = .none) { + public func selectRow(at index: Index?, scrollPosition: UITableView.ScrollPosition = .none, animated: Bool = true) { if let index = index { tableView.selectRow( - at: IndexPath(row: index, section: 0), animated: true, scrollPosition: scrollPosition + at: IndexPath(row: index, section: 0), animated: animated, scrollPosition: scrollPosition ) selectedRowIndices.insert(index) } else {