             }
             if let newSelection = pdfView.document?.findString(text, fromSelection: pdfView.currentSelection, withOptions: options) {
                 pdfView.go(to: newSelection)
+                if let page = newSelection.pages.first {
+                    
+                    let selectionBounds = newSelection.bounds(for: page)
+                    let selectionBoundsInView = pdfView.convert(selectionBounds, from: page)
+                    
+                    if let scrollView = pdfView.scrollView {
+                        if selectionBoundsInView.origin.y < pdfView.safeAreaInsets.top {
+                            let offsetNeedToFix = pdfView.safeAreaInsets.top - selectionBoundsInView.origin.y
+                            scrollView.contentOffset = CGPoint(x: scrollView.contentOffset.x, y: scrollView.contentOffset.y - offsetNeedToFix)
+                        } else if selectionBoundsInView.origin.y + selectionBoundsInView.height > pdfView.frame.size.height - pdfView.safeAreaInsets.bottom {
+                            let offsetNeedToFix = (selectionBoundsInView.origin.y + selectionBoundsInView.height) - (pdfView.frame.size.height - pdfView.safeAreaInsets.bottom)
+                            scrollView.contentOffset = CGPoint(x: scrollView.contentOffset.x, y: scrollView.contentOffset.y + offsetNeedToFix)
+                        }
+                    }
+                }
                 pdfView.setCurrentSelection(newSelection, animate: true)
             } else {
                 // for workaround: clear selected if no real search results returned

    
    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        searchBarText = searchBar.text
        if isPageExchangedForRTL {
            searchText(withOptions: .backwards)
        } else {
            searchText(withOptions: .regularExpression)
        }
    }
    
    // UISearchControllerDelegate

    // MARK: - IBAction

    @IBAction func searchPrevious(_ sender: UIBarButtonItem) {
        searchText(withOptions: [.regularExpression, .backwards])
    }
    
    @IBAction func searchNext(_ sender: UIBarButtonItem) {
        searchText(withOptions: [.regularExpression])
    }
    
    @IBAction func shareAction() {
