omshanti
omshanti

Reputation: 33

Hide indicator scroll in PDFKit

This is my PDFView config. I want to show PDF viewer with horizontal scroll, show single page.

But PDFView show the scroll indicator and it is not correct. I want to hide scroll indicator. My question is how to hide it or how to make its scroll position correct?

Horizontal scroll indicator visible at bottom of screen
pdfView.autoScales = true
pdfView.displaysPageBreaks = true
pdfView.displayMode = .singlePageContinuous
pdfView.usePageViewController(true)
pdfView.displayDirection = .horizontal
pdfView.minScaleFactor = 1
pdfView.document = pdfDocument

Upvotes: 3

Views: 1226

Answers (4)

Vanita L.
Vanita L.

Reputation: 1325

Since PDFview is not giving access to its scrolling elements, the trick of accessing subviews of pdfview may work for you.

Chain of subviews when printed on console looks like:

(lldb) po pdfView.subviews
▿ 1 element
    - 0 : <_UIPageViewControllerContentView: 0x7fec7411d9f0; frame = (0 0; 414 808); clipsToBounds = YES; opaque = NO; autoresize = W+H; layer = <CALayer: 0x600001b33720>>

(lldb) po pdfView.subviews.first?.subviews.first
▿ Optional<UIView>
  - some : <_UIQueuingScrollView: 0x7fec7483aa00; frame = (0 0; 414 736); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x6000015153e0>; layer = <CALayer: 0x600001b33740>; contentOffset: {414, 0}; contentSize: {1242, 736}; adjustedContentInset: {0, 0, 0, 0}>

(lldb) 

So based on this, I have tested below code and it worked at my end. Try with code:

    let pdfScrollView = pdfView.subviews.first?.subviews.first as? UIScrollView
    pdfScrollView?.showsHorizontalScrollIndicator = false  // if pdf view scroll direction is horizontal
    pdfScrollView?.showsVerticalScrollIndicator = false // if pdf view scroll direction is vertical

Edit:

The view's hierarchy is been updated and scrollView is not at the first place Please refer answer from @Deepansh for getting scrollView object: https://stackoverflow.com/a/77080309/2677551

Upvotes: 3

Deepansh Jagga
Deepansh Jagga

Reputation: 109

The UIScrollView is not inside the first subview anymore (as mentioned in previous answers), so iterate through all subviews to find the UIScrollView

extension PDFView {   
    func hideScrollViewIndicator(){
        for subview in subviews {
            if let scrollView = subview as? UIScrollView {
                scrollView.showsVerticalScrollIndicator = false
                scrollView.showsHorizontalScrollIndicator = false
                return
            }
        }
    }
}

and use it like this:

pdfView.hideScrollViewIndicator()

Upvotes: 1

Alexander Khitev
Alexander Khitev

Reputation: 6851

I implemented an extension and then use like this, please have a look.

import Foundation
import PDFKit

extension PDFView {
    var scrollView: UIScrollView? {
        guard let pageViewControllerContentView = subviews.first else { return nil }
        for view in pageViewControllerContentView.subviews {
            guard let scrollView = view as? UIScrollView else { continue }
            return scrollView
        }

        return nil
    }
}

and then I use

override func layoutSubviews() {
    super.layoutSubviews()
    pdfView.scrollView?.showsVerticalScrollIndicator = false
    pdfView.scrollView?.showsHorizontalScrollIndicator = false
}

Another more straightforward way is: before your app starts showing an element with PDFView (for example UIViewController) you need to set this

UIScrollView.appearance().showsVerticalScrollIndicator = false
UIScrollView.appearance().showsHorizontalScrollIndicator = false

and it will work for you. then when you'll need to dismiss UIViewController or another element with the PDView, you can return back previous settings

UIScrollView.appearance().showsVerticalScrollIndicator = true
UIScrollView.appearance().showsHorizontalScrollIndicator = true

and it should work.

Upvotes: 0

Vahid
Vahid

Reputation: 3496

I think it's better solution to find UIScrollView of subviews:

It's tested on SwiftUI 2, 3 and working well.

UIViewRepresentable > makeUIView function

Finding UIScrollView and modifications:

let pdfView = PDFView()    
for view in pdfView.subviews {
    if let scrollView = findUIScrollView(of: view) {
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
    }
}
    
func findUIScrollView(of uiView: UIView) -> UIScrollView? {
    if let scrollView = uiView as? UIScrollView {
        return scrollView
    }
        
    for view in uiView.subviews {
       if let scrollView = view as? UIScrollView {
           return scrollView
        }
            
        if !view.subviews.isEmpty {
            return findUIScrollView(of: view)
        }
    }
    return nil
}

Upvotes: 0

Related Questions