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 alloc] initWithFrame: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];