Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6894809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:49:44+00:00 2026-05-27T06:49:44+00:00

The Problem I have a UIWebView inside my iPad application which I need to

  • 0

The Problem

I have a UIWebView inside my iPad application which I need to zoom programmatically, but without the use of gestures/taps. The app. has a “+” and a “-” button for zooming in-and-out in user-defined increments (it is an app. for the visually impaired).

This +/- zoom-button functionality previously worked 100% fine when my app. used a UIImageView inside of a UIScrollView (instead of the WebView). The ImageView was the actual view which was zoomed, and the CenterPoint was calculated for the ScrollView’s zoomToRect method.

I now have a WebView, which I know contains a ScrollView as a subview. I tried adapting the code which previously worked with the ImageView/ScrollView combo to instead zoom the WebView’s ScrollView, but it is no longer calculating the CenterPoint correctly for zoomToRect:.

What Happens:

The WebView zooms correctly in-terms of the zoom-level, but the center point is always wrong. For some reason, the screen always zooms-in on the top-left every time.

Another odd problem, is that after the very first time you zoom-in, you cannot scroll in the WebView past the visible portion of the screen. If you try to, it shows a bit of the content past the bounds of the currently visible area, but it instantly snaps-back.

What I have tried:

I am trying to zoom the UIWebView’s ScrollView.

I create a pointer to the ScrollView, and set “self” as its delegate. I then setup various variables, such as scrSize (the size of the view to zoom) and ZoomHandler (explained below):

- (void)viewDidLoad {
  // ... Various UIWebView setup ...    
  [self LoadURL];

  // Zooming & scrollview setup
  zoomHandler = [[ZoomHandler alloc] initWithZoomLevel: ZOOM_STEP];
  scrSize = CGPointMake(self.WebView.frame.size.width, self.WebView.frame.size.height);

  scrollView = [WebView.subviews objectAtIndex:0];
  [scrollView setTag:ZOOM_VIEW_TAG];
  [scrollView setMinimumZoomScale:MINIMUM_SCALE];
  [scrollView setZoomScale:1];
  [scrollView setMaximumZoomScale:10];
  scrollView.bounces = FALSE;
  scrollView.bouncesZoom = FALSE;
  scrollView.clipsToBounds = NO;
  [scrollView setDelegate:self];

  [super viewDidLoad];
}

To override the WebView’s default zooming limitations, I inject this Javascript into the loaded webpage in the webViewDidFinishLoad: method:

function increaseMaxZoomFactor() {
  var element = document.createElement('meta');
  element.name = "viewport";
  element.content = "maximum-scale=10 minimum-scale=1 initial-scale=1 user-scalable=yes width=device-width height=device-height;"
  var head = document.getElementsByTagName('head')[0];
  head.appendChild(element);
}

CenterPoint Code:

This code is used to calculate the CenterPoint to pass into zoomToRect:. This worked 100% fine when I was zooming an ImageView inside of a ScrollView.

-(IBAction)zoomOut {
    float newScale = [scrollView zoomScale] / ZOOM_STEP;
    if( [scrollView zoomScale] > MINIMUM_SCALE) {
        [self handleZoomWith:newScale andZoomType: FALSE];
    }
}

-(IBAction)zoomIn {
    float newScale = [scrollView zoomScale] * ZOOM_STEP;
    if( [scrollView zoomScale] < MAXIMUM_SCALE){
        [self handleZoomWith:newScale andZoomType: TRUE];
    }
}

-(void)handleZoomWith: (float) newScale andZoomType:(BOOL) isZoomIn {
    CGPoint newOrigin = [zoomHandler getNewOriginFromViewLocation: [scrollView contentOffset] 
                                                        viewSize: scrSize andZoomType: isZoomIn];
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:newOrigin];
    [scrollView zoomToRect:zoomRect animated:YES];
}

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
    CGRect zoomRect;

    //  At a zoom scale of 1.0, it would be the size of the scrollView's bounds.
    //  As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [WebView frame].size.height / scale;
    zoomRect.size.width  = [WebView frame].size.width  / scale;

    // Choose an origin so as to get the right center.
    zoomRect.origin.x = center.x / scale;
    zoomRect.origin.y = center.y / scale;

    return zoomRect;
}

/**
 Determine the origin [THIS IS INSIDE ZOOMHANDLER]
 */
-(CGPoint) getNewOriginFromViewLocation: (CGPoint) oldOrigin viewSize: (CGPoint) viewSize andZoomType:(BOOL) isZoomIn {

    // Calculate original center (add the half of the width/height of the screen)
    float oldCenterX = oldOrigin.x + (viewSize.x / 2);
    float oldCenterY = oldOrigin.y + (viewSize.y / 2);

    // Xalculate the new center
    CGPoint newCenter;
    if(isZoomIn) {
        newCenter = CGPointMake(oldCenterX * zoomLevel, oldCenterY * zoomLevel);
    } else {
        newCenter = CGPointMake(oldCenterX / zoomLevel, oldCenterY / zoomLevel);
    }

    // Calculate the new origin (deduct the half of the width/height of the screen)
    float newOriginX = newCenter.x - (viewSize.x / 2);
    float newOriginY = newCenter.y - (viewSize.y / 2);

    return CGPointMake(newOriginX, newOriginY);
}

Does anyone have any idea why the CenterPoint is not being calculated correctly? Any help would be GREATLY appreciated; I have been stuck on this for a week now.

Thanks,
Alex

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:49:44+00:00Added an answer on May 27, 2026 at 6:49 am

    Have you tried implementing the zoom using JavaScript rather than the UIWebView’s scrollview ?

    I believe you can zoom the webview by calling:

    [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = 5.0;"]; (use your ZOOM_STEP as the value)

    You could also calculate the center of the browser window using JS:

    posY = getScreenCenterY();
    posX = getScreenCenterX();
    
    function getScreenCenterY() {
        var y = 0;
        y = getScrollOffset()+(getInnerHeight()/2);
        return(y);
    }
    
    function getScreenCenterX() {
        return(document.body.clientWidth/2);
    }
    
    function getInnerHeight() {
        var y;
        if (self.innerHeight) // all except Explorer
        {
            y = self.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        // Explorer 6 Strict Mode
        {
            y = document.documentElement.clientHeight;
        }
        else if (document.body) // other Explorers
        {
            y = document.body.clientHeight;
        }
        return(y);
    }
    
    function getScrollOffset() {
        var y;
        if (self.pageYOffset) // all except Explorer
        {
            y = self.pageYOffset;
        }
        else if (document.documentElement &&
    document.documentElement.scrollTop) // Explorer 6 Strict
        {
            y = document.documentElement.scrollTop;
        }
        else if (document.body) // all other Explorers
        {
            y = document.body.scrollTop;
        }
        return(y);
    }
    

    (source: http://sliceofcake.wordpress.com/2007/09/13/use-javascript-to-find-the-center-of-the-browser/)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery-mobile application that is running inside a UIWebView in an iphone
I have a problem while opening a pdf in a UIWebView. Zoom in and
I have problem with UIWebView delay when the load image from url. In my
I'm having a really confusing problem. I have a UIWebView that is contained within
Problem: Have made a small mail program which works perfectly on my developer pc
Problem: I have a table that prints out vertical but I would like it
I have the same problem as described here: UIWebView doesn't scroll to the bottom
i have use a UIWebview in IOS 5. I try to set contenteditable=true to
I have a UIWebView which is embedded in a UIScrollView. The webView is resized
I have a UIWebView that loads a YouTube video. The problem is, that if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.