iPhone UIWebView width does not fit after zooming operation + UIInterfaceOrientation change

前端 未结 8 1538
庸人自扰
庸人自扰 2020-12-02 19:20

I created a bare bones iPhone app with a UIWebView (Scales Page to Fit = YES, shouldAutorotateToInterfaceOrientation = YES) and loaded a webpage, e.g. https://stackoverflow.

8条回答
  •  长情又很酷
    2020-12-02 20:05

    I am posting this because i have also faced the same problem and here i am following the M Penades Approach.M Penades 's Answer woks good only for case if user does not Skew(pinch Out) the Webview then rotate the device and repeat this process .then Content Size of UiwebView gets reduce gradually. so that was the issue came in M Penades Answer. so I have fixed that issue too and my code is as below.

    1) For This I set the Pinch Gesture so that when User Skew The UIwebView could check the Scaled size of UIwebView. //One This Please import The UIGestureRecognizerDelegate Protocol in '.h file'

         //call below method in ViewDidLoad Method for setting the Pinch gesture 
    
    - (void)setPinchgesture
    {
         UIPinchGestureRecognizer * pinchgesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(didPinchWebView:)];
    
        [pinchgesture setDelegate:self];
    
        [htmlWebView addGestureRecognizer:pinchgesture];
        [pinchgesture release];
       // here htmlWebView is WebView user zoomingIn/Out 
    
    }
       //Allow The allow simultaneous recognition
    
      - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer   *)otherGestureRecognizer
      {
         return YES;
      }
    

    Returning YES is guaranteed to allow simultaneous recognition. returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture's delegate may return YES

     -(void)didPinchWebView:(UIPinchGestureRecognizer*)gestsure
       {
       //check if the Scaled Fator is same is normal scaling factor the allow set Flag True.
        if(gestsure.scale<=1.0)
        {
        isPinchOut = TRUE;
    
        }
        else// otherwise Set false
        {
        isPinchOut = FALSE;
       }
      NSLog(@"Hello Pinch %f",gestsure.scale);
    }
    

    If User Hase Pinch In/Out The Web View in that Case Just Set THat Zooming Factor . SO that WebView Can Adjust Its ContentSize as Oreintaion Changed.

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation   duration:(NSTimeInterval)duration {
    
       //Allow the Execution of below code when user has Skewed the UIWebView and Adjust the Content Size of UiwebView.
    
      if(isPinchOut){
      CGFloat ratioAspect = htmlWebView.bounds.size.width/htmlWebView.bounds.size.height;
      switch (toInterfaceOrientation) {
        case UIInterfaceOrientationPortraitUpsideDown:
        case UIInterfaceOrientationPortrait:
            // Going to Portrait mode
            for (UIScrollView *scroll in [htmlWebView subviews]) { //we get the scrollview
                // Make sure it really is a scroll view and reset the zoom scale.
                if ([scroll respondsToSelector:@selector(setZoomScale:)]){
                    scroll.minimumZoomScale = scroll.minimumZoomScale/ratioAspect;
                    scroll.maximumZoomScale = scroll.maximumZoomScale/ratioAspect;
                    [scroll setZoomScale:(scroll.zoomScale/ratioAspect) animated:YES];
                }
            }
            break;
    
        default:
            // Going to Landscape mode
            for (UIScrollView *scroll in [htmlWebView subviews]) { //we get the scrollview
                // Make sure it really is a scroll view and reset the zoom scale.
                if ([scroll respondsToSelector:@selector(setZoomScale:)]){
                    scroll.minimumZoomScale = scroll.minimumZoomScale *ratioAspect;
                    scroll.maximumZoomScale = scroll.maximumZoomScale *ratioAspect;
                    [scroll setZoomScale:(scroll.zoomScale*ratioAspect) animated:YES];
                }
            }
            break;
         }
      }
    
    }
    

    This Works perfectly for even user skew the UIWebView.

提交回复
热议问题