Search code examples
iphoneioscocoa-touchuiwebviewuiwebviewdelegate

UIWebView loading some black screen in the right side of Landscape View


I am facing this weird problem not in a normal behavior. I am loading the images from a url in the web-view. If I change the Orientation from portrait to landscape and landscape to portrait no problem. But In Portrait If I do some zoom-in , zoom-out and double tap the image to fit in the screen, then if I change to Landscape now, I can see there is white/black screen is coming in the right side.

I have googled about this issue, I tried many ways like Opaque property, clear color etc. Even I tried my own way to resolve this , but I failed. How to fix the issue ?


Solution

  • I am posting this because i have also faced the same problem ,I have fixed that issue too and my code is as below.Here Below is the Whole Code Which can Solve the Issue.Need To properly Put ALl code in ViewControler

    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.