How to "stick" a UIView to a page on a PDFViewCtrl on iOS

109 views
Skip to first unread message

James

unread,
Dec 3, 2013, 9:28:52 PM12/3/13
to pdfne...@googlegroups.com
I’m implementing a functionality on the reader so I can add on the top of the PDFReader for example a UIButton that has a background image. 
The idea is to add that UIButton at a certain point on one specific page and when the user zooms in, zooms out or scrolls through that page the UIButton resizes and moves with the pages so it appears always on the top of the same point in the pdf.

What I’m doing right now is to add to the PDFViewCtrl's public ivar ContainerView the UIButton like this. 

   UIButton *button = [[UIButton allocinitWithFrame:CGRectMake(x, y, width, height)];
   [m_pdfViewCtrl->ContainerView addSubview:button]; 

The problem I’m facing is that when I zoom in,  zoom out or scroll through the page, the UIButton is all the time in the same place on the screen and doesn’t resize at the same time with the pdf page.
What I was doing to solve that problem was to implement in the following delegate method this code to resize the frame of the UIButton manually but I’m having trouble to resize it to the correct place because it doesn’t work as I expect.

- (void)onLayoutChanged {

    

    // Get the subviews of the view
    NSArray *subviews = [m_pdfViewCtrl->ContainerView subviews];

    

    for (UIView *subView in subviews) {

        

        if ([subView isKindOfClass:[UIButton class]]) {
            [subView setFrame:CGRectMake(x * fltZoom, y * fltZoom, width * fltZoom, height * fltZoom)];

            

        }
    }
}


I would like to know first, if I’m using the correct approach in order to achieve what I’m trying to do, and in that case, how can I resize the UIButton properly so it is always resizing and scrolling with the page.

----------------------------

A:

Are you sure that the button doesn't move correctly when scrolling and pinch-zooming? Adding code as follows should work. with the button moving and zooming as if it were "pasted" onto the PDF document.

UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"TEST TEST TEST" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor redColor]];
button.frame = CGRectMake(100,100,200,200);
[myPDFViewCtrl->ContainerView addSubview:button];

However what you will need to implement is logic for when the user releases their fingers after a zoom. When this occurs the ContainerView will change size to reflect the new total scrollable area of the document. If the item that is a subview of ContainerView is supposed to be stationary relative to a page (which is likely to be the case), the PDFViewCtrl methods ConvScreenPtToPagePt and ConvPagePtToScreenPt will come in useful in calculating the new frame that the button should have when a subview of ContainerView (which is in screen coordinates).

Sree Harsha

unread,
Oct 20, 2016, 9:18:56 PM10/20/16
to PDFTron PDFNet SDK
When i implement same thing in swift, i am getting error like (Value of type 'myPDFViewCtrl' has no member 'ContainerView')
Reply all
Reply to author
Forward
0 new messages